From 3ee89ac42ca103544f9ef65a9532c6e667c768ca Mon Sep 17 00:00:00 2001 From: NekitoSP Date: Fri, 14 Aug 2015 22:03:42 +0500 Subject: [PATCH] selectedRows fix for $showHeader=false #9074 --- framework/assets/yii.gridView.js | 2 +- framework/grid/CheckboxColumn.php | 28 +++++++++++++++++++--------- framework/grid/GridView.php | 9 ++++++--- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/framework/assets/yii.gridView.js b/framework/assets/yii.gridView.js index 81abb3e2de..fcf76e7aa3 100644 --- a/framework/assets/yii.gridView.js +++ b/framework/assets/yii.gridView.js @@ -119,7 +119,7 @@ var $grid = $(this); var id = $(this).attr('id'); gridData[id].selectionColumn = options.name; - if (!options.multiple) { + if (!options.multiple || !options.checkAll) { return; } var checkAll = "#" + id + " input[name='" + options.checkAll + "']"; diff --git a/framework/grid/CheckboxColumn.php b/framework/grid/CheckboxColumn.php index 7adfc68b4c..ed412bd562 100644 --- a/framework/grid/CheckboxColumn.php +++ b/framework/grid/CheckboxColumn.php @@ -81,6 +81,15 @@ class CheckboxColumn extends Column if (substr_compare($this->name, '[]', -2, 2)) { $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);"); } /** @@ -91,18 +100,10 @@ class CheckboxColumn extends Column */ protected function renderHeaderCellContent() { - $name = rtrim($this->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 { + $name = $this->getHeaderCheckBoxName(); return Html::checkBox($name, false, ['class' => 'select-on-check-all']); } } @@ -123,4 +124,13 @@ class CheckboxColumn extends Column return Html::checkbox($this->name, !empty($options['checked']), $options); } + + /** + * Returns header checkbox name + * @return string header checkbox name + */ + private function getHeaderCheckBoxName() + { + return rtrim($this->name, '[]') . '_all'; + } } diff --git a/framework/grid/GridView.php b/framework/grid/GridView.php index 4e7d22b1fc..eda17746ed 100644 --- a/framework/grid/GridView.php +++ b/framework/grid/GridView.php @@ -272,6 +272,11 @@ class GridView extends BaseListView $this->filterRowOptions['id'] = $this->options['id'] . '-filters'; } + $id = $this->options['id']; + $options = Json::htmlEncode($this->getClientOptions()); + $view = $this->getView(); + $view->registerJs("jQuery('#$id').yiiGridView($options);"); + $this->initColumns(); } @@ -280,11 +285,9 @@ class GridView extends BaseListView */ public function run() { - $id = $this->options['id']; - $options = Json::htmlEncode($this->getClientOptions()); $view = $this->getView(); GridViewAsset::register($view); - $view->registerJs("jQuery('#$id').yiiGridView($options);"); + parent::run(); }