You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.0 KiB

12 years ago
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\widgets\grid;
12 years ago
use Closure;
use yii\base\InvalidConfigException;
use yii\helpers\Html;
12 years ago
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class CheckboxColumn extends Column
{
12 years ago
public $name;
public $checkboxOptions = array();
public $multiple = true;
12 years ago
public function init()
{
12 years ago
parent::init();
if (empty($this->name)) {
throw new InvalidConfigException('The "name" property must be set.');
12 years ago
}
12 years ago
if (substr($this->name, -2) !== '[]') {
$this->name .= '[]';
12 years ago
}
}
/**
* Renders the header cell content.
12 years ago
* 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
12 years ago
*/
protected function renderHeaderCellContent()
{
12 years ago
$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);");
12 years ago
12 years ago
if ($this->header !== null || !$this->multiple) {
return parent::renderHeaderCellContent();
12 years ago
} else {
12 years ago
return Html::checkBox($name, false, array('class' => 'select-on-check-all'));
12 years ago
}
}
/**
* Renders the data cell content.
12 years ago
* @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
12 years ago
*/
12 years ago
protected function renderDataCellContent($model, $index)
12 years ago
{
12 years ago
if ($this->checkboxOptions instanceof Closure) {
$options = call_user_func($this->checkboxOptions, $model, $index, $this);
12 years ago
} else {
12 years ago
$options = $this->checkboxOptions;
12 years ago
}
12 years ago
return Html::checkbox($this->name, !empty($options['checked']), $options);
12 years ago
}
}