我是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>
答案 0 :(得分:0)
一些简单的方法
您可以将controller/route/action
与URL
进行比较,如果它们匹配,请添加带有yii2帮助器的活动类:
向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'],);
?>
如果使用导航窗口小部件,则可以利用active
和visible
属性:
<?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'),
],
],
]);
?>