Returns the sort direction of the specified attribute in the current request.
public getAttributeOrder ( string $attribute ) : boolean | null | ||
$attribute | string | the attribute name |
return | boolean | null | Sort direction of the attribute. Can be either `SORT_ASC` for ascending order or `SORT_DESC` for descending order. Null is returned if the attribute is invalid or does not need to be sorted. |
public function testGetAttributeOrder() { $sort = new Sort(['attributes' => ['age', 'name' => ['asc' => ['first_name' => SORT_ASC, 'last_name' => SORT_ASC], 'desc' => ['first_name' => SORT_DESC, 'last_name' => SORT_DESC]]], 'params' => ['sort' => 'age,-name'], 'enableMultiSort' => true]); $this->assertEquals(SORT_ASC, $sort->getAttributeOrder('age')); $this->assertEquals(SORT_DESC, $sort->getAttributeOrder('name')); $this->assertNull($sort->getAttributeOrder('xyz')); }
/** * Generates a hyperlink that can be clicked to cause sorting. * @param Sort $sort the current Sort instance * @param string $attribute the attribute name * @param array $options additional HTML attributes for the hyperlink tag. * There is one special attribute `label` which will be used as the label of the hyperlink. * If this is not set, the label defined in [[attributes]] will be used. * If no label is defined, [[\yii\helpers\Inflector::camel2words()]] will be called to get a label. * Note that it will not be HTML-encoded. * @return string */ public static function sortLink(Sort $sort, $attribute, $options = []) { if (($direction = $sort->getAttributeOrder($attribute)) !== null) { $class = $direction === SORT_DESC ? 'current-sort desc' : 'current-sort asc'; $icon = $direction === SORT_DESC ? 'glyphicon-sort-by-alphabet-alt' : 'glyphicon-sort-by-alphabet'; if (isset($options['class'])) { $options['class'] .= ' ' . $class; } else { $options['class'] = $class; } } else { $icon = 'glyphicon-sort-by-alphabet'; if (isset($options['class'])) { $options['class'] .= ' asc'; } else { $options['class'] = 'asc'; } } $url = $sort->createUrl($attribute); $options['data-sort'] = $sort->createSortParam($attribute); if (isset($options['label'])) { $label = $options['label']; unset($options['label']); } else { if (isset($sort->attributes[$attribute]['label'])) { $label = $sort->attributes[$attribute]['label']; } else { $label = Inflector::camel2words($attribute); } } $label .= ' <span class="glyphicon ' . $icon . '"></span>'; return Html::a($label, $url, $options); }