Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

127 lines
4.3KB

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