|
|
|
@ -10,6 +10,7 @@ namespace yii\grid;
|
|
|
|
|
use Closure; |
|
|
|
|
use yii\base\InvalidConfigException; |
|
|
|
|
use yii\helpers\Html; |
|
|
|
|
use yii\helpers\Json; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* CheckboxColumn displays a column of checkboxes in a grid view. |
|
|
|
@ -82,14 +83,7 @@ class CheckboxColumn extends Column
|
|
|
|
|
$this->name .= '[]'; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$name = $this->grid->showHeader ? $this->getHeaderCheckBoxName() : NULL; |
|
|
|
|
$id = $this->grid->options['id']; |
|
|
|
|
$options = json_encode([ |
|
|
|
|
'name' => $this->name, |
|
|
|
|
'multiple' => $this->multiple, |
|
|
|
|
'checkAll' => $name, |
|
|
|
|
], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
|
|
|
|
$this->grid->getView()->registerJs("jQuery('#$id').yiiGridView('setSelectionColumn', $options);"); |
|
|
|
|
$this->registerClientScript(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -100,28 +94,10 @@ class CheckboxColumn extends Column
|
|
|
|
|
*/ |
|
|
|
|
protected function renderHeaderCellContent() |
|
|
|
|
{ |
|
|
|
|
$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'; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$id = $this->grid->options['id']; |
|
|
|
|
$options = json_encode([ |
|
|
|
|
'name' => $this->name, |
|
|
|
|
'multiple' => $this->multiple, |
|
|
|
|
'checkAll' => $name, |
|
|
|
|
], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
|
|
|
|
$this->grid->getView()->registerJs("jQuery('#$id').yiiGridView('setSelectionColumn', $options);"); |
|
|
|
|
|
|
|
|
|
if ($this->header !== null || !$this->multiple) { |
|
|
|
|
return parent::renderHeaderCellContent(); |
|
|
|
|
} else { |
|
|
|
|
return Html::checkbox($name, false, ['class' => 'select-on-check-all']); |
|
|
|
|
return Html::checkbox($this->getHeaderCheckBoxName(), false, ['class' => 'select-on-check-all']); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -137,7 +113,7 @@ class CheckboxColumn extends Column
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!isset($options['value'])) { |
|
|
|
|
$options['value'] = is_array($key) ? json_encode($key, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) : $key; |
|
|
|
|
$options['value'] = is_array($key) ? Json::encode($key) : $key; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return Html::checkbox($this->name, !empty($options['checked']), $options); |
|
|
|
@ -146,9 +122,35 @@ class CheckboxColumn extends Column
|
|
|
|
|
/** |
|
|
|
|
* Returns header checkbox name |
|
|
|
|
* @return string header checkbox name |
|
|
|
|
* @since 2.0.8 |
|
|
|
|
*/ |
|
|
|
|
private function getHeaderCheckBoxName() |
|
|
|
|
protected function getHeaderCheckBoxName() |
|
|
|
|
{ |
|
|
|
|
return rtrim($this->name, '[]') . '_all'; |
|
|
|
|
$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, |
|
|
|
|
'multiple' => $this->multiple, |
|
|
|
|
'checkAll' => $this->grid->showHeader ? $this->getHeaderCheckBoxName() : null, |
|
|
|
|
]); |
|
|
|
|
$this->grid->getView()->registerJs("jQuery('#$id').yiiGridView('setSelectionColumn', $options);"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|