CheckboxColumn.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. }
  100. return Html::checkbox($this->getHeaderCheckBoxName(), false, ['class' => 'select-on-check-all']);
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. protected function renderDataCellContent($model, $key, $index)
  106. {
  107. if ($this->content !== null) {
  108. return parent::renderDataCellContent($model, $key, $index);
  109. }
  110. if ($this->checkboxOptions instanceof Closure) {
  111. $options = call_user_func($this->checkboxOptions, $model, $key, $index, $this);
  112. } else {
  113. $options = $this->checkboxOptions;
  114. }
  115. if (!isset($options['value'])) {
  116. $options['value'] = is_array($key) ? Json::encode($key) : $key;
  117. }
  118. if ($this->cssClass !== null) {
  119. Html::addCssClass($options, $this->cssClass);
  120. }
  121. return Html::checkbox($this->name, !empty($options['checked']), $options);
  122. }
  123. /**
  124. * Returns header checkbox name.
  125. * @return string header checkbox name
  126. * @since 2.0.8
  127. */
  128. protected function getHeaderCheckBoxName()
  129. {
  130. $name = $this->name;
  131. if (substr_compare($name, '[]', -2, 2) === 0) {
  132. $name = substr($name, 0, -2);
  133. }
  134. if (substr_compare($name, ']', -1, 1) === 0) {
  135. $name = substr($name, 0, -1) . '_all]';
  136. } else {
  137. $name .= '_all';
  138. }
  139. return $name;
  140. }
  141. /**
  142. * Registers the needed JavaScript.
  143. * @since 2.0.8
  144. */
  145. public function registerClientScript()
  146. {
  147. $id = $this->grid->options['id'];
  148. $options = Json::encode([
  149. 'name' => $this->name,
  150. 'class' => $this->cssClass,
  151. 'multiple' => $this->multiple,
  152. 'checkAll' => $this->grid->showHeader ? $this->getHeaderCheckBoxName() : null,
  153. ]);
  154. $this->grid->getView()->registerJs("jQuery('#$id').yiiGridView('setSelectionColumn', $options);");
  155. }
  156. }