Browse Source

Add ability to turn the sorting off for a clicked column in GridView with multisort (#18826)

* Add ability to turn the sorting off for a clicked column in GridView with multisort

* Update Sort.php

* Fix SortTest

* Update CHANGELOG.md

Co-authored-by: Bizley <pawel@positive.codes>
tags/2.0.44
Andrew 3 years ago committed by GitHub
parent
commit
463a67bc73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      framework/CHANGELOG.md
  2. 15
      framework/data/Sort.php
  3. 24
      tests/framework/data/SortTest.php

2
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)

15
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];
}

24
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'));
}
/**

Loading…
Cancel
Save