如何在Yii2中为点击按钮提供活动课堂

时间:2019-05-28 17:26:21

标签: yii2

我是Yii2的新成员。我默认将Yii2高级模板与新模板集成在一起。我需要使用活动类从侧栏单击菜单。在我的代码下面。我该怎么做?请帮助我

<li class="">
   <?= Html::a('<i class="glyphicon glyphicon-education"></i><span>Суд жараёни</span>', ['law1/']) ?>
</li>
<li class="">
   <?= Html::a('<i class="glyphicon glyphicon-check"></i><span>МИБ</span>', ['law2/']) ?>
</li>
<li class="">
   <?= Html::a('<i class="glyphicon glyphicon-ruble"></i><span>Прократура</span>', ['law3/']) ?>
</li>
<li class="">
   <?= Html::a('<i class="glyphicon glyphicon-bitcoin"></i><span>Банк</span>', ['law4/']) ?>
</li>

1 个答案:

答案 0 :(得分:0)

一些简单的方法

您可以将controller/route/actionURL进行比较,如果它们匹配,请添加带有yii2帮助器的活动类:

  1. 向html链接添加简单类:

    <?php
    use yii\helpers\Html;
    $controller =  Yii::$app->controller->id;
    $options = ['class' => ['btn btn-md btn-default']];
    if ($controller == 'batch') {
        Html::addCssClass($options, ['active']);
    }
    echo $link = Html::a('SomeLink', ['batch/view'], $options);
    echo $link = Html::a('SomeLink', ['batch/view'],);
    ?>
    
  2. 如果使用导航窗口小部件,则可以利用activevisible属性:

    <?php
    use mdm\admin\components\Helper;
    $controller =  Yii::$app->controller->id;
    echo kartik\sidenav\SideNav::widget([
        'type' => SideNav::TYPE_DEFAULT,
        'items' => [
            [
                'url' => ['supplier/index'],
                'label' => 'Suppliers',
                //'icon' => 'th-list',
                'active' => $controller == 'supplier',
                'visible' => Helper::checkRoute('supplier/index'),
            ],
            [
                'url' => ['site/index'],
                'label' => 'Sites',
                //'icon' => 'th-list',
                'active' => $controller == 'site',
                'visible' => Helper::checkRoute('site/index'),
            ],
        ],
    ]);
    ?>