function ($model, $key, $index, $column) { * return ['value' => $model->name]; * } * ``` * * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. */ public $checkboxOptions = []; /** * @var bool whether it is possible to select multiple rows. Defaults to `true`. */ public $multiple = true; /** * @var string the css class that will be used to find the checkboxes. * @since 2.0.9 */ public $cssClass; /** * {@inheritdoc} * @throws \yii\base\InvalidConfigException if [[name]] is not set. */ 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(); } /** * 行头输出内容 * @return type */ protected function renderHeaderCellContent() { return; } /** * {@inheritdoc} */ protected function renderDataCellContent($model, $key, $index) { if ($this->content !== null) { return parent::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); } /** * Returns header checkbox name. * @return string header checkbox name * @since 2.0.8 */ 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; } /** * Registers the needed JavaScript. * @since 2.0.8 */ 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);"); } }