您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

172 行
6.0KB

  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\grid;
  8. use Yii;
  9. use Closure;
  10. use yii\helpers\Html;
  11. use yii\helpers\Url;
  12. /**
  13. * ActionColumn is a column for the [[GridView]] widget that displays buttons for viewing and manipulating the items.
  14. *
  15. * To add an ActionColumn to the gridview, add it to the [[GridView::columns|columns]] configuration as follows:
  16. *
  17. * ```php
  18. * 'columns' => [
  19. * // ...
  20. * [
  21. * 'class' => ActionColumn::className(),
  22. * // you may configure additional properties here
  23. * ],
  24. * ]
  25. * ```
  26. *
  27. * @author Qiang Xue <qiang.xue@gmail.com>
  28. * @since 2.0
  29. */
  30. class ActionColumn extends Column
  31. {
  32. /**
  33. * @var string the ID of the controller that should handle the actions specified here.
  34. * If not set, it will use the currently active controller. This property is mainly used by
  35. * [[urlCreator]] to create URLs for different actions. The value of this property will be prefixed
  36. * to each action name to form the route of the action.
  37. */
  38. public $controller;
  39. /**
  40. * @var string the template used for composing each cell in the action column.
  41. * Tokens enclosed within curly brackets are treated as controller action IDs (also called *button names*
  42. * in the context of action column). They will be replaced by the corresponding button rendering callbacks
  43. * specified in [[buttons]]. For example, the token `{view}` will be replaced by the result of
  44. * the callback `buttons['view']`. If a callback cannot be found, the token will be replaced with an empty string.
  45. *
  46. * As an example, to only have the view, and update button you can add the ActionColumn to your GridView columns as follows:
  47. *
  48. * ```
  49. * ['class' => 'yii\grid\ActionColumn', 'template' => '{view} {update}'],
  50. * ```
  51. *
  52. * @see buttons
  53. */
  54. public $template = '{view} {update} {delete}';
  55. /**
  56. * @var array button rendering callbacks. The array keys are the button names (without curly brackets),
  57. * and the values are the corresponding button rendering callbacks. The callbacks should use the following
  58. * signature:
  59. *
  60. * ```php
  61. * function ($url, $model, $key) {
  62. * // return the button HTML code
  63. * }
  64. * ```
  65. *
  66. * where `$url` is the URL that the column creates for the button, `$model` is the model object
  67. * being rendered for the current row, and `$key` is the key of the model in the data provider array.
  68. *
  69. * You can add further conditions to the button, for example only display it, when the model is
  70. * editable (here assuming you have a status field that indicates that):
  71. *
  72. * ```php
  73. * [
  74. * 'update' => function ($url, $model, $key) {
  75. * return $model->status === 'editable' ? Html::a('Update', $url) : '';
  76. * };
  77. * ],
  78. * ```
  79. */
  80. public $buttons = [];
  81. /**
  82. * @var callable a callback that creates a button URL using the specified model information.
  83. * The signature of the callback should be the same as that of [[createUrl()]].
  84. * If this property is not set, button URLs will be created using [[createUrl()]].
  85. */
  86. public $urlCreator;
  87. /**
  88. * @inheritdoc
  89. */
  90. public function init()
  91. {
  92. parent::init();
  93. $this->initDefaultButtons();
  94. }
  95. /**
  96. * Initializes the default button rendering callbacks.
  97. */
  98. protected function initDefaultButtons()
  99. {
  100. if (!isset($this->buttons['view'])) {
  101. $this->buttons['view'] = function ($url, $model, $key) {
  102. return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', $url, [
  103. 'title' => Yii::t('yii', 'View'),
  104. 'data-pjax' => '0',
  105. ]);
  106. };
  107. }
  108. if (!isset($this->buttons['update'])) {
  109. $this->buttons['update'] = function ($url, $model, $key) {
  110. return Html::a('<span class="glyphicon glyphicon-pencil"></span>', $url, [
  111. 'title' => Yii::t('yii', 'Update'),
  112. 'data-pjax' => '0',
  113. ]);
  114. };
  115. }
  116. if (!isset($this->buttons['delete'])) {
  117. $this->buttons['delete'] = function ($url, $model, $key) {
  118. return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, [
  119. 'title' => Yii::t('yii', 'Delete'),
  120. 'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'),
  121. 'data-method' => 'post',
  122. 'data-pjax' => '0',
  123. ]);
  124. };
  125. }
  126. }
  127. /**
  128. * Creates a URL for the given action and model.
  129. * This method is called for each button and each row.
  130. * @param string $action the button name (or action ID)
  131. * @param \yii\db\ActiveRecord $model the data model
  132. * @param mixed $key the key associated with the data model
  133. * @param integer $index the current row index
  134. * @return string the created URL
  135. */
  136. public function createUrl($action, $model, $key, $index)
  137. {
  138. if ($this->urlCreator instanceof Closure) {
  139. return call_user_func($this->urlCreator, $action, $model, $key, $index);
  140. } else {
  141. $params = is_array($key) ? $key : ['id' => (string) $key];
  142. $params[0] = $this->controller ? $this->controller . '/' . $action : $action;
  143. return Url::toRoute($params);
  144. }
  145. }
  146. /**
  147. * @inheritdoc
  148. */
  149. protected function renderDataCellContent($model, $key, $index)
  150. {
  151. return preg_replace_callback('/\\{([\w\-\/]+)\\}/', function ($matches) use ($model, $key, $index) {
  152. $name = $matches[1];
  153. if (isset($this->buttons[$name])) {
  154. $url = $this->createUrl($name, $model, $key, $index);
  155. return call_user_func($this->buttons[$name], $url, $model, $key);
  156. } else {
  157. return '';
  158. }
  159. }, $this->template);
  160. }
  161. }