Yii2 Nav 小部件:活动项目

Yii2 Nav widget : active item

我有 Nav 个项目:

[
    'label' => 'All',
    'url' => ['project/index'],
],
[
    'label' => 'Done',
    'url' => ['project/index', 'assigned' => 'done'],
],

但是当我转到 project/index&assigned=done 时,它​​们都有一个 active class。仅当项目的 url 严格等于 $route 值时,我如何强制附加此 class?

Nav 小部件将在其路由和参数匹配 $route (if not set, it will use the route of the current request) and $params 时激活一个项目(如果未设置,它将使用 $_GET)。

如果路线是 project/index(看看 here),您的第一个项目将始终处于活动状态。

你应该试试这个,例如:

[
    'label' => 'All',
    'url' => ['project/index', 'assigned' => 'not-done'],
],
[
    'label' => 'Done',
    'url' => ['project/index', 'assigned' => 'done'],
],

详细了解 how Nav widget set an item active or not

$actionId = $this->context->action->id; // the id of the actual action

$items = [
    ['label' => 'All', 'active' => $actionId === 'index',
        'url' => ['/project/index'],
    ['label' => 'Done', 'active' => $actionId === 'index' 
         && \Yii::$app->request->get('assigned') === 'done',  
        'url' => ['/project/index', 'assigned' => 'done']]
];