Yii2 Bootstrap 3
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.

82 lines
2.1 KiB

11 years ago
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\grid;
11 years ago
11 years ago
use Closure;
use yii\base\InvalidConfigException;
use yii\helpers\Html;
11 years ago
/**
* 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
* ~~~
*
11 years ago
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class CheckboxColumn extends Column
{
public $name = 'selection';
public $checkboxOptions = [];
11 years ago
public $multiple = true;
11 years ago
public function init()
{
11 years ago
parent::init();
if (empty($this->name)) {
throw new InvalidConfigException('The "name" property must be set.');
11 years ago
}
11 years ago
if (substr($this->name, -2) !== '[]') {
$this->name .= '[]';
11 years ago
}
}
/**
* Renders the header cell content.
11 years ago
* The default implementation simply renders [[header]].
11 years ago
* This method may be overridden to customize the rendering of the header cell.
* @return string the rendering result
11 years ago
*/
protected function renderHeaderCellContent()
{
11 years ago
$name = rtrim($this->name, '[]') . '_all';
$id = $this->grid->options['id'];
$options = json_encode([
11 years ago
'name' => $this->name,
'multiple' => $this->multiple,
'checkAll' => $name,
]);
11 years ago
$this->grid->getView()->registerJs("jQuery('#$id').yiiGridView('setSelectionColumn', $options);");
11 years ago
11 years ago
if ($this->header !== null || !$this->multiple) {
return parent::renderHeaderCellContent();
11 years ago
} else {
return Html::checkBox($name, false, ['class' => 'select-on-check-all']);
11 years ago
}
}
/**
* @inheritdoc
11 years ago
*/
protected function renderDataCellContent($model, $key, $index)
11 years ago
{
11 years ago
if ($this->checkboxOptions instanceof Closure) {
$options = call_user_func($this->checkboxOptions, $model, $key, $index, $this);
11 years ago
} else {
11 years ago
$options = $this->checkboxOptions;
11 years ago
}
11 years ago
return Html::checkbox($this->name, !empty($options['checked']), $options);
11 years ago
}
}