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
|
|
|
|
8
|
|
|
namespace yii\grid; |
9
|
|
|
|
10
|
|
|
use Closure; |
11
|
|
|
use yii\base\InvalidConfigException; |
12
|
|
|
use yii\helpers\Html; |
13
|
|
|
use yii\helpers\Json; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* CheckboxColumn displays a column of checkboxes in a grid view. |
17
|
|
|
* |
18
|
|
|
* To add a CheckboxColumn to the [[GridView]], add it to the [[GridView::columns|columns]] configuration as follows: |
19
|
|
|
* |
20
|
|
|
* ```php |
21
|
|
|
* 'columns' => [ |
22
|
|
|
* // ... |
23
|
|
|
* [ |
24
|
|
|
* 'class' => 'yii\grid\CheckboxColumn', |
25
|
|
|
* // you may configure additional properties here |
26
|
|
|
* ], |
27
|
|
|
* ] |
28
|
|
|
* ``` |
29
|
|
|
* |
30
|
|
|
* Users may click on the checkboxes to select rows of the grid. The selected rows may be |
31
|
|
|
* obtained by calling the following JavaScript code: |
32
|
|
|
* |
33
|
|
|
* ```javascript |
34
|
|
|
* var keys = $('#grid').yiiGridView('getSelectedRows'); |
35
|
|
|
* // keys is an array consisting of the keys associated with the selected rows |
36
|
|
|
* ``` |
37
|
|
|
* |
38
|
|
|
* For more details and usage information on CheckboxColumn, see the [guide article on data widgets](guide:output-data-widgets). |
39
|
|
|
* |
40
|
|
|
* @author Qiang Xue <[email protected]> |
41
|
|
|
* @since 2.0 |
42
|
|
|
*/ |
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() |
84
|
|
|
{ |
85
|
3 |
|
parent::init(); |
86
|
3 |
|
if (empty($this->name)) { |
87
|
|
|
throw new InvalidConfigException('The "name" property must be set.'); |
88
|
|
|
} |
89
|
3 |
|
if (substr_compare($this->name, '[]', -2, 2)) { |
90
|
3 |
|
$this->name .= '[]'; |
91
|
|
|
} |
92
|
|
|
|
93
|
3 |
|
$this->registerClientScript(); |
94
|
3 |
|
} |
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() |
103
|
|
|
{ |
104
|
1 |
|
if ($this->header !== null || !$this->multiple) { |
105
|
|
|
return parent::renderHeaderCellContent(); |
106
|
|
|
} |
107
|
|
|
|
108
|
1 |
|
return Html::checkbox($this->getHeaderCheckBoxName(), false, ['class' => 'select-on-check-all']); |
109
|
|
|
} |
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() |
143
|
|
|
{ |
144
|
3 |
|
$name = $this->name; |
145
|
3 |
|
if (substr_compare($name, '[]', -2, 2) === 0) { |
146
|
3 |
|
$name = substr($name, 0, -2); |
147
|
|
|
} |
148
|
3 |
|
if (substr_compare($name, ']', -1, 1) === 0) { |
149
|
1 |
|
$name = substr($name, 0, -1) . '_all]'; |
150
|
|
|
} else { |
151
|
3 |
|
$name .= '_all'; |
152
|
|
|
} |
153
|
|
|
|
154
|
3 |
|
return $name; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Registers the needed JavaScript. |
159
|
|
|
* @since 2.0.8 |
160
|
|
|
*/ |
161
|
3 |
|
public function registerClientScript() |
162
|
|
|
{ |
163
|
3 |
|
$id = $this->grid->options['id']; |
164
|
3 |
|
$options = Json::encode([ |
165
|
3 |
|
'name' => $this->name, |
166
|
3 |
|
'class' => $this->cssClass, |
167
|
3 |
|
'multiple' => $this->multiple, |
168
|
3 |
|
'checkAll' => $this->grid->showHeader ? $this->getHeaderCheckBoxName() : null, |
169
|
|
|
]); |
170
|
3 |
|
$this->grid->getView()->registerJs("jQuery('#$id').yiiGridView('setSelectionColumn', $options);"); |
171
|
3 |
|
} |
172
|
|
|
} |
173
|
|
|
|