CheckboxColumn.php 2.31 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

8
namespace yii\grid;
Qiang Xue committed
9

Qiang Xue committed
10 11 12 13
use Closure;
use yii\base\InvalidConfigException;
use yii\helpers\Html;

Qiang Xue committed
14
/**
15 16 17 18 19 20 21 22 23
 * 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
 * ~~~
 *
Qiang Xue committed
24 25 26 27 28
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class CheckboxColumn extends Column
{
29
	public $name = 'selection';
Alexander Makarov committed
30
	public $checkboxOptions = [];
Qiang Xue committed
31 32
	public $multiple = true;

Qiang Xue committed
33 34 35

	public function init()
	{
Qiang Xue committed
36 37 38
		parent::init();
		if (empty($this->name)) {
			throw new InvalidConfigException('The "name" property must be set.');
Qiang Xue committed
39
		}
Qiang Xue committed
40 41
		if (substr($this->name, -2) !== '[]') {
			$this->name .= '[]';
Qiang Xue committed
42 43 44 45 46
		}
	}

	/**
	 * Renders the header cell content.
Qiang Xue committed
47 48 49
	 * 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
Qiang Xue committed
50 51 52
	 */
	protected function renderHeaderCellContent()
	{
Qiang Xue committed
53 54
		$name = rtrim($this->name, '[]') . '_all';
		$id = $this->grid->options['id'];
Alexander Makarov committed
55
		$options = json_encode([
Qiang Xue committed
56 57 58
			'name' => $this->name,
			'multiple' => $this->multiple,
			'checkAll' => $name,
Alexander Makarov committed
59
		]);
Qiang Xue committed
60
		$this->grid->getView()->registerJs("jQuery('#$id').yiiGridView('setSelectionColumn', $options);");
Qiang Xue committed
61

Qiang Xue committed
62 63
		if ($this->header !== null || !$this->multiple) {
			return parent::renderHeaderCellContent();
Qiang Xue committed
64
		} else {
Alexander Makarov committed
65
			return Html::checkBox($name, false, ['class' => 'select-on-check-all']);
Qiang Xue committed
66 67 68 69 70
		}
	}

	/**
	 * Renders the data cell content.
Qiang Xue committed
71 72 73
	 * @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
Qiang Xue committed
74
	 */
Qiang Xue committed
75
	protected function renderDataCellContent($model, $index)
Qiang Xue committed
76
	{
Qiang Xue committed
77 78
		if ($this->checkboxOptions instanceof Closure) {
			$options = call_user_func($this->checkboxOptions, $model, $index, $this);
Qiang Xue committed
79
		} else {
Qiang Xue committed
80
			$options = $this->checkboxOptions;
Qiang Xue committed
81
		}
Qiang Xue committed
82
		return Html::checkbox($this->name, !empty($options['checked']), $options);
Qiang Xue committed
83 84
	}
}