mirror of
				https://github.com/yiisoft/yii2.git
				synced 2025-11-04 14:46:19 +08:00 
			
		
		
		
	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>
This commit is contained in:
		@ -4,6 +4,8 @@ Yii Framework 2 Change Log
 | 
				
			|||||||
2.0.44 under development
 | 
					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)
 | 
					- Bug #18646: Remove stale identity data from session if `IdentityInterface::findIdentity()` returns `null` (mikehaertl)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -438,14 +438,25 @@ class Sort extends BaseObject
 | 
				
			|||||||
        $definition = $this->attributes[$attribute];
 | 
					        $definition = $this->attributes[$attribute];
 | 
				
			||||||
        $directions = $this->getAttributeOrders();
 | 
					        $directions = $this->getAttributeOrders();
 | 
				
			||||||
        if (isset($directions[$attribute])) {
 | 
					        if (isset($directions[$attribute])) {
 | 
				
			||||||
 | 
					            if ($this->enableMultiSort) {
 | 
				
			||||||
 | 
					                if ($directions[$attribute] === SORT_ASC) {
 | 
				
			||||||
 | 
					                    $direction = SORT_DESC;
 | 
				
			||||||
 | 
					                } else {
 | 
				
			||||||
 | 
					                    $direction = null;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            } else {
 | 
				
			||||||
                $direction = $directions[$attribute] === SORT_DESC ? SORT_ASC : SORT_DESC;
 | 
					                $direction = $directions[$attribute] === SORT_DESC ? SORT_ASC : SORT_DESC;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            unset($directions[$attribute]);
 | 
					            unset($directions[$attribute]);
 | 
				
			||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
            $direction = isset($definition['default']) ? $definition['default'] : SORT_ASC;
 | 
					            $direction = isset($definition['default']) ? $definition['default'] : SORT_ASC;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if ($this->enableMultiSort) {
 | 
					        if ($this->enableMultiSort) {
 | 
				
			||||||
 | 
					            if ($direction !== null) {
 | 
				
			||||||
                $directions = array_merge([$attribute => $direction], $directions);
 | 
					                $directions = array_merge([$attribute => $direction], $directions);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
            $directions = [$attribute => $direction];
 | 
					            $directions = [$attribute => $direction];
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
				
			|||||||
@ -163,8 +163,30 @@ class SortTest extends TestCase
 | 
				
			|||||||
            'route' => 'site/index',
 | 
					            'route' => 'site/index',
 | 
				
			||||||
        ]);
 | 
					        ]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $sort->params = ['sort' => 'age,-name'];
 | 
				
			||||||
 | 
					        $sort->getAttributeOrders(true);
 | 
				
			||||||
        $this->assertEquals('-age,-name', $sort->createSortParam('age'));
 | 
					        $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'));
 | 
					        $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()
 | 
					    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=-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'));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user