【问题标题】:Yii2 Nav widget : active itemYii2 Nav 小部件:活动项目
【发布时间】:2016-08-29 19:24:00
【问题描述】:

我有 Nav 的物品:

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

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

【问题讨论】:

    标签: yii2 nav


    【解决方案1】:

    Nav 小部件将在其路由和参数匹配$route(如果未设置,它将使用当前请求的路由)和$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的信息。

    【讨论】:

    • 感谢您的链接。它们很有用。
    【解决方案2】:
    $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']]
    ];
    

    【讨论】:

      猜你喜欢
      • 2015-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-27
      • 2017-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多