選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

167 行
5.1KB

  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 Closure;
  9. use yii\base\InvalidConfigException;
  10. use yii\helpers\Html;
  11. use yii\helpers\Json;
  12. /**
  13. * CheckboxColumn displays a column of checkboxes in a grid view.
  14. *
  15. * To add a CheckboxColumn to the [[GridView]], add it to the [[GridView::columns|columns]] configuration as follows:
  16. *
  17. * ```php
  18. * 'columns' => [
  19. * // ...
  20. * [
  21. * 'class' => 'yii\grid\CheckboxColumn',
  22. * // you may configure additional properties here
  23. * ],
  24. * ]
  25. * ```
  26. *
  27. * Users may click on the checkboxes to select rows of the grid. The selected rows may be
  28. * obtained by calling the following JavaScript code:
  29. *
  30. * ```javascript
  31. * var keys = $('#grid').yiiGridView('getSelectedRows');
  32. * // keys is an array consisting of the keys associated with the selected rows
  33. * ```
  34. *
  35. * @author Qiang Xue <qiang.xue@gmail.com>
  36. * @since 2.0
  37. */
  38. class CheckboxColumn extends Column
  39. {
  40. /**
  41. * @var string the name of the input checkbox input fields. This will be appended with `[]` to ensure it is an array.
  42. */
  43. public $name = 'selection';
  44. /**
  45. * @var array|\Closure the HTML attributes for checkboxes. This can either be an array of
  46. * attributes or an anonymous function ([[Closure]]) that returns such an array.
  47. * The signature of the function should be the following: `function ($model, $key, $index, $column)`.
  48. * Where `$model`, `$key`, and `$index` refer to the model, key and index of the row currently being rendered
  49. * and `$column` is a reference to the [[CheckboxColumn]] object.
  50. * A function may be used to assign different attributes to different rows based on the data in that row.
  51. * Specifically if you want to set a different value for the checkbox
  52. * you can use this option in the following way (in this example using the `name` attribute of the model):
  53. *
  54. * ```php
  55. * 'checkboxOptions' => function ($model, $key, $index, $column) {
  56. * return ['value' => $model->name];
  57. * }
  58. * ```
  59. *
  60. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  61. */
  62. public $checkboxOptions = [];
  63. /**
  64. * @var boolean whether it is possible to select multiple rows. Defaults to `true`.
  65. */
  66. public $multiple = true;
  67. /**
  68. * @var string the css class that will be used to find the checkboxes.
  69. * @since 2.0.9
  70. */
  71. public $cssClass;
  72. /**
  73. * @inheritdoc
  74. * @throws \yii\base\InvalidConfigException if [[name]] is not set.
  75. */
  76. public function init()
  77. {
  78. parent::init();
  79. if (empty($this->name)) {
  80. throw new InvalidConfigException('The "name" property must be set.');
  81. }
  82. if (substr_compare($this->name, '[]', -2, 2)) {
  83. $this->name .= '[]';
  84. }
  85. $this->registerClientScript();
  86. }
  87. /**
  88. * Renders the header cell content.
  89. * The default implementation simply renders [[header]].
  90. * This method may be overridden to customize the rendering of the header cell.
  91. * @return string the rendering result
  92. */
  93. protected function renderHeaderCellContent()
  94. {
  95. if ($this->header !== null || !$this->multiple) {
  96. return parent::renderHeaderCellContent();
  97. } else {
  98. return Html::checkbox($this->getHeaderCheckBoxName(), false, ['class' => 'select-on-check-all']);
  99. }
  100. }
  101. /**
  102. * @inheritdoc
  103. */
  104. protected function renderDataCellContent($model, $key, $index)
  105. {
  106. if ($this->checkboxOptions instanceof Closure) {
  107. $options = call_user_func($this->checkboxOptions, $model, $key, $index, $this);
  108. } else {
  109. $options = $this->checkboxOptions;
  110. }
  111. if (!isset($options['value'])) {
  112. $options['value'] = is_array($key) ? Json::encode($key) : $key;
  113. }
  114. if ($this->cssClass !== null) {
  115. Html::addCssClass($options, $this->cssClass);
  116. }
  117. return Html::checkbox($this->name, !empty($options['checked']), $options);
  118. }
  119. /**
  120. * Returns header checkbox name
  121. * @return string header checkbox name
  122. * @since 2.0.8
  123. */
  124. protected function getHeaderCheckBoxName()
  125. {
  126. $name = $this->name;
  127. if (substr_compare($name, '[]', -2, 2) === 0) {
  128. $name = substr($name, 0, -2);
  129. }
  130. if (substr_compare($name, ']', -1, 1) === 0) {
  131. $name = substr($name, 0, -1) . '_all]';
  132. } else {
  133. $name .= '_all';
  134. }
  135. return $name;
  136. }
  137. /**
  138. * Registers the needed JavaScript
  139. * @since 2.0.8
  140. */
  141. public function registerClientScript()
  142. {
  143. $id = $this->grid->options['id'];
  144. $options = Json::encode([
  145. 'name' => $this->name,
  146. 'class' => $this->cssClass,
  147. 'multiple' => $this->multiple,
  148. 'checkAll' => $this->grid->showHeader ? $this->getHeaderCheckBoxName() : null,
  149. ]);
  150. $this->grid->getView()->registerJs("jQuery('#$id').yiiGridView('setSelectionColumn', $options);");
  151. }
  152. }