|
- <?php
-
-
- namespace yii\grid;
-
- use Closure;
- use yii\base\InvalidConfigException;
- use yii\helpers\Html;
- use yii\helpers\Json;
-
-
- class CheckboxColumn extends Column
- {
-
-
- public $name = 'selection';
-
-
- public $checkboxOptions = [];
-
-
- public $multiple = true;
-
-
- public $cssClass;
-
-
-
-
- public function init()
- {
- parent::init();
- if (empty($this->name)) {
- throw new InvalidConfigException('The "name" property must be set.');
- }
- if (substr_compare($this->name, '[]', -2, 2)) {
- $this->name .= '[]';
- }
-
- $this->registerClientScript();
- }
-
-
-
- protected function renderHeaderCellContent()
- {
- if ($this->header !== null || !$this->multiple) {
- return parent::renderHeaderCellContent();
- } else {
- return Html::checkbox($this->getHeaderCheckBoxName(), false, ['class' => 'select-on-check-all']);
- }
- }
-
-
-
- protected function renderDataCellContent($model, $key, $index)
- {
- if ($this->checkboxOptions instanceof Closure) {
- $options = call_user_func($this->checkboxOptions, $model, $key, $index, $this);
- } else {
- $options = $this->checkboxOptions;
- }
-
- if (!isset($options['value'])) {
- $options['value'] = is_array($key) ? Json::encode($key) : $key;
- }
-
- if ($this->cssClass !== null) {
- Html::addCssClass($options, $this->cssClass);
- }
-
- return Html::checkbox($this->name, !empty($options['checked']), $options);
- }
-
-
-
- protected function getHeaderCheckBoxName()
- {
- $name = $this->name;
- if (substr_compare($name, '[]', -2, 2) === 0) {
- $name = substr($name, 0, -2);
- }
- if (substr_compare($name, ']', -1, 1) === 0) {
- $name = substr($name, 0, -1) . '_all]';
- } else {
- $name .= '_all';
- }
-
- return $name;
- }
-
-
-
- public function registerClientScript()
- {
- $id = $this->grid->options['id'];
- $options = Json::encode([
- 'name' => $this->name,
- 'class' => $this->cssClass,
- 'multiple' => $this->multiple,
- 'checkAll' => $this->grid->showHeader ? $this->getHeaderCheckBoxName() : null,
- ]);
- $this->grid->getView()->registerJs("jQuery('#$id').yiiGridView('setSelectionColumn', $options);");
- }
- }
|