CheckboxColumn.php 5.3 KB

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