1 | <?php |
||
43 | class CheckboxColumn extends Column |
||
44 | { |
||
45 | /** |
||
46 | * @var string the name of the input checkbox input fields. This will be appended with `[]` to ensure it is an array. |
||
47 | */ |
||
48 | public $name = 'selection'; |
||
49 | /** |
||
50 | * @var array|\Closure the HTML attributes for checkboxes. This can either be an array of |
||
51 | * attributes or an anonymous function ([[Closure]]) that returns such an array. |
||
52 | * The signature of the function should be the following: `function ($model, $key, $index, $column)`. |
||
53 | * Where `$model`, `$key`, and `$index` refer to the model, key and index of the row currently being rendered |
||
54 | * and `$column` is a reference to the [[CheckboxColumn]] object. |
||
55 | * A function may be used to assign different attributes to different rows based on the data in that row. |
||
56 | * Specifically if you want to set a different value for the checkbox |
||
57 | * you can use this option in the following way (in this example using the `name` attribute of the model): |
||
58 | * |
||
59 | * ```php |
||
60 | * 'checkboxOptions' => function ($model, $key, $index, $column) { |
||
61 | * return ['value' => $model->name]; |
||
62 | * } |
||
63 | * ``` |
||
64 | * |
||
65 | * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
||
66 | */ |
||
67 | public $checkboxOptions = []; |
||
68 | /** |
||
69 | * @var bool whether it is possible to select multiple rows. Defaults to `true`. |
||
70 | */ |
||
71 | public $multiple = true; |
||
72 | /** |
||
73 | * @var string the css class that will be used to find the checkboxes. |
||
74 | * @since 2.0.9 |
||
75 | */ |
||
76 | public $cssClass; |
||
77 | |||
78 | |||
79 | /** |
||
80 | * {@inheritdoc} |
||
81 | * @throws \yii\base\InvalidConfigException if [[name]] is not set. |
||
82 | */ |
||
83 | 3 | public function init() |
|
95 | |||
96 | /** |
||
97 | * Renders the header cell content. |
||
98 | * The default implementation simply renders [[header]]. |
||
99 | * This method may be overridden to customize the rendering of the header cell. |
||
100 | * @return string the rendering result |
||
101 | */ |
||
102 | 1 | protected function renderHeaderCellContent() |
|
110 | |||
111 | /** |
||
112 | * {@inheritdoc} |
||
113 | */ |
||
114 | 2 | protected function renderDataCellContent($model, $key, $index) |
|
115 | { |
||
116 | 2 | if ($this->content !== null) { |
|
117 | 1 | return parent::renderDataCellContent($model, $key, $index); |
|
118 | } |
||
119 | |||
120 | 1 | if ($this->checkboxOptions instanceof Closure) { |
|
121 | 1 | $options = call_user_func($this->checkboxOptions, $model, $key, $index, $this); |
|
122 | } else { |
||
123 | 1 | $options = $this->checkboxOptions; |
|
124 | } |
||
125 | |||
126 | 1 | if (!isset($options['value'])) { |
|
127 | 1 | $options['value'] = is_array($key) ? Json::encode($key) : $key; |
|
128 | } |
||
129 | |||
130 | 1 | if ($this->cssClass !== null) { |
|
131 | Html::addCssClass($options, $this->cssClass); |
||
132 | } |
||
133 | |||
134 | 1 | return Html::checkbox($this->name, !empty($options['checked']), $options); |
|
135 | } |
||
136 | |||
137 | /** |
||
138 | * Returns header checkbox name. |
||
139 | * @return string header checkbox name |
||
140 | * @since 2.0.8 |
||
141 | */ |
||
142 | 3 | protected function getHeaderCheckBoxName() |
|
156 | |||
157 | /** |
||
158 | * Registers the needed JavaScript. |
||
159 | * @since 2.0.8 |
||
160 | */ |
||
161 | 3 | public function registerClientScript() |
|
172 | } |
||
173 |