diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 20b4ca3..605b1df 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -4,6 +4,8 @@ Yii Framework 2 Change Log 2.0.44 under development ------------------------ + +- Enh #18826: Add ability to turn the sorting off for a clicked column in GridView with multisort (ditibal) - Bug #18646: Remove stale identity data from session if `IdentityInterface::findIdentity()` returns `null` (mikehaertl) diff --git a/framework/data/Sort.php b/framework/data/Sort.php index 0e62064..f402af2 100644 --- a/framework/data/Sort.php +++ b/framework/data/Sort.php @@ -438,14 +438,25 @@ class Sort extends BaseObject $definition = $this->attributes[$attribute]; $directions = $this->getAttributeOrders(); if (isset($directions[$attribute])) { - $direction = $directions[$attribute] === SORT_DESC ? SORT_ASC : SORT_DESC; + if ($this->enableMultiSort) { + if ($directions[$attribute] === SORT_ASC) { + $direction = SORT_DESC; + } else { + $direction = null; + } + } else { + $direction = $directions[$attribute] === SORT_DESC ? SORT_ASC : SORT_DESC; + } + unset($directions[$attribute]); } else { $direction = isset($definition['default']) ? $definition['default'] : SORT_ASC; } if ($this->enableMultiSort) { - $directions = array_merge([$attribute => $direction], $directions); + if ($direction !== null) { + $directions = array_merge([$attribute => $direction], $directions); + } } else { $directions = [$attribute => $direction]; } diff --git a/tests/framework/data/SortTest.php b/tests/framework/data/SortTest.php index ff50a98..89cd8a0 100644 --- a/tests/framework/data/SortTest.php +++ b/tests/framework/data/SortTest.php @@ -163,8 +163,30 @@ class SortTest extends TestCase 'route' => 'site/index', ]); + $sort->params = ['sort' => 'age,-name']; + $sort->getAttributeOrders(true); $this->assertEquals('-age,-name', $sort->createSortParam('age')); + $this->assertEquals('age', $sort->createSortParam('name')); + + $sort->params = ['sort' => 'age']; + $sort->getAttributeOrders(true); + $this->assertEquals('-age', $sort->createSortParam('age')); + + $sort->params = ['sort' => '-age']; + $sort->getAttributeOrders(true); + $this->assertEquals('', $sort->createSortParam('age')); + + $sort->params = ['sort' => 'age']; + $sort->getAttributeOrders(true); $this->assertEquals('name,age', $sort->createSortParam('name')); + + $sort->params = ['sort' => 'name,age']; + $sort->getAttributeOrders(true); + $this->assertEquals('-name,age', $sort->createSortParam('name')); + + $sort->params = ['sort' => '-name,age']; + $sort->getAttributeOrders(true); + $this->assertEquals('age', $sort->createSortParam('name')); } public function testCreateUrl() @@ -192,7 +214,7 @@ class SortTest extends TestCase ]); $this->assertEquals('/index.php?r=site%2Findex&sort=-age%2C-name', $sort->createUrl('age')); - $this->assertEquals('/index.php?r=site%2Findex&sort=name%2Cage', $sort->createUrl('name')); + $this->assertEquals('/index.php?r=site%2Findex&sort=age', $sort->createUrl('name')); } /**