1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\grid;
use Closure;
use yii\base\InvalidConfigException;
use yii\helpers\Html;
/**
* CheckboxColumn displays a column of checkboxes in a grid view.
* Users may click on the checkboxes to select rows of the grid. The selected rows may be
* obtained by calling the following JavaScript code:
*
* ~~~
* var keys = $('#grid').yiiGridView('getSelectedRows');
* // keys is an array consisting of the keys associated with the selected rows
* ~~~
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class CheckboxColumn extends Column
{
public $name = 'selection';
public $checkboxOptions = array();
public $multiple = true;
public function init()
{
parent::init();
if (empty($this->name)) {
throw new InvalidConfigException('The "name" property must be set.');
}
if (substr($this->name, -2) !== '[]') {
$this->name .= '[]';
}
}
/**
* Renders the header cell content.
* The default implementation simply renders {@link header}.
* This method may be overridden to customize the rendering of the header cell.
* @return string the rendering result
*/
protected function renderHeaderCellContent()
{
$name = rtrim($this->name, '[]') . '_all';
$id = $this->grid->options['id'];
$options = json_encode(array(
'name' => $this->name,
'multiple' => $this->multiple,
'checkAll' => $name,
));
$this->grid->getView()->registerJs("jQuery('#$id').yiiGridView('setSelectionColumn', $options);");
if ($this->header !== null || !$this->multiple) {
return parent::renderHeaderCellContent();
} else {
return Html::checkBox($name, false, array('class' => 'select-on-check-all'));
}
}
/**
* Renders the data cell content.
* @param mixed $model the data model
* @param integer $index the zero-based index of the data model among the models array returned by [[dataProvider]].
* @return string the rendering result
*/
protected function renderDataCellContent($model, $index)
{
if ($this->checkboxOptions instanceof Closure) {
$options = call_user_func($this->checkboxOptions, $model, $index, $this);
} else {
$options = $this->checkboxOptions;
}
return Html::checkbox($this->name, !empty($options['checked']), $options);
}
}