public link ( string $attribute, array $options = [] ) : string | ||
$attribute | string | the attribute name by which the data should be sorted by. |
$options | array | 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. |
Результат | string | the generated hyperlink |
public function link($attribute, $options = [])
{
if (($direction = $this->getAttributeOrder($attribute)) !== null) {
$class = $direction === SORT_DESC ? 'desc' : 'asc';
if (isset($options['class'])) {
$options['class'] .= ' ' . $class;
} else {
$options['class'] = $class;
}
}
$url = $this->createUrl($attribute);
$options['data-sort'] = $this->createSortParam($attribute);
if (isset($options['label'])) {
$label = $options['label'];
unset($options['label']);
} else {
if (isset($this->attributes[$attribute]['label'])) {
$label = $this->attributes[$attribute]['label'];
} else {
$label = Inflector::camel2words($attribute);
}
}
return Html::a($label, $url, $options);
}
public function testLink() { $this->mockApplication(); $manager = new UrlManager(['baseUrl' => '/index.php', 'cache' => null]); $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, 'urlManager' => $manager, 'route' => 'site/index']); $this->assertEquals('<a class="asc" href="/index.php?r=site%2Findex&sort=-age%2C-name" data-sort="-age,-name">Age</a>', $sort->link('age')); }