I'm setting up a small page, so i removed /site/ from the URL. Most pages will be static pages on the site controller, so this looks cleaner.
The menu widget is configured as follows:
$menuItems = [
['label' => 'Home', 'url' => ['/']],
['label' => 'About', 'url' => ['/about']],
['label' => 'Projects', 'url' => ['/projects']],
];
And the config rule is
'rules' => [
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
'<action:\w+>' => 'site/<action>',
],
Now my problem is that my menu item links are no longer marked as active (css .active). What controlls this, and how can I change it?
Solved.
Change the menuItems like this
$menuItems = [
['label' => 'Home', 'url' => ['/'], 'active' => $this->context->route == 'site/index'],
['label' => 'About', 'url' => ['/about'], 'active' => $this->context->route == 'site/about'],
['label' => 'Projects', 'url' => ['/projects'], 'active' => $this->context->route == 'site/projects'],
];
You should to use 'url' => [controller/action] to get this thing done. And this config will be overwrited by you rules. So, and then class .active will be added automatically.
You can use in_array method to check current URL. In array we need add all urls in which menu is in active state.
'active' => in_array($this->context->route,['items/index','items/create','items/update']),
If we open List page, create page or update page, Item menu will be active.
We can use Controller id to check current controller
'active' => Yii::$app->controller->id == 'items',
I use this code in yii2 for best menu:
\yii\widgets\Menu::widget([
'submenuTemplate' => "\n<ul class='top-nav-menu'>\n{items}\n</ul>\n",
'activateParents' => false,
'activeCssClass' => 'active',
'id' => 'sandwich-menu',
['label' => Yii::t('app', 'Education'), 'url' => ['/site/education'] ],
[
'label' => Yii::t('app', 'Entertainment'),
'url' => '#',
'items' => [
['label' => Yii::t('app', 'Games'), 'url' => ['/site/games']],
['label' => Yii::t('app', 'Music'), 'url' => ['/site/music'] ],
['label' => Yii::t('app', 'Videos'), 'url' => ['/site/videos'] ],
['label' => Yii::t('app', 'Chat & Forum'), 'url' => ['/site/forum'] ],
['label' => Yii::t('app', 'Clubs'), 'url' => ['/site/clubs'] ]
]
],
['label' => Yii::t('app', 'News'), 'url' => ['/site/news']],
['label' => Yii::t('app', 'Contact'), 'url' => ['/site/gencol'] ]
]
])
Actually, as soovorow suggested, it should work with
$menuItems = [
['label' => 'Home', 'url' => ['/']],
['label' => 'About', 'url' => ['/site/about']],
['label' => 'Projects', 'url' => ['/site/projects']],
];
the problem was that the controlled name was not explicit in
$menuItems = [
['label' => 'Home', 'url' => ['/']],
['label' => 'About', 'url' => ['/about']],
['label' => 'Projects', 'url' => ['/projects']],
];
Related
I have this gridview
I want to replace the blank space on the status with All, so that it will have All, Active, Inactive
View:Gridview
[
'class' => '\pheme\grid\ToggleColumn',
'contentOptions' => ['class' => 'text-center'],
'attribute'=>'is_status',
'enableAjax' => false,
'filter'=>['1'=>'InActive', '0'=>'Active'],
],
How do I achieve this?
Use filterInputOptions
[
'class' => '\pheme\grid\ToggleColumn',
'contentOptions' => ['class' => 'text-center'],
'attribute' => 'is_status',
'enableAjax' => false,
'filter' => ['1' => 'InActive', '0' => 'Active'],
'filterInputOptions' => ['class' => 'form-control', 'id' => null, 'prompt' => 'All'],
],
I have a menu like this
<?php
NavBar::begin([
'brandLabel' => Html::img('#web/images/cennos1.png', ['alt'=>Yii::$app->name]),
'brandUrl' => Yii::$app->homeUrl,
'brandOptions' => ['style' => 'margin-top:-7px;'],
'options' => [
'class' => 'navbar-inverse navbar-fixed-top',
],
]);
$menuItems = [];
$menuItems[] = ['label' => 'Login', 'url' => ['/site/login']];
$menuItems[] = [
'label' => 'Teams',
'items' => [
foreach ($teams as $team) {
['label' => '' . $team->name .'', 'url' => ['team/preview','id' => $team->id]],
}
],
];
I tried to use foreach loop like that to list all teams as a dropdown menu for guest users to see but it didn't work.
Please help me with this. Sorry for my bad English.
Thank you.
This may not be the best way, but it works for me.
function items($teams)
{
$items = [];
foreach ($teams as $team) {
array_push($items, ['label' => '' . $team->name .'', 'url' => Url::to(['team/preview', 'id' => $team->id])]);
}
return $items;
}
NavBar::begin([
'brandLabel' => Html::img('#web/images/cennos1.png', ['alt'=>Yii::$app->name]),
'brandUrl' => Yii::$app->homeUrl,
'brandOptions' => ['style' => 'margin-top:-7px;'],
'options' => [
'class' => 'navbar-inverse navbar-fixed-top',
],
]);
$menuItems = [];
$menuItems[] = ['label' => 'Login', 'url' => ['/site/login']];
$menuItems[] = [
'label' => 'Teams',
'items' => items($teams)
];
Hope it helps,
I have followed the guidelines given by GitHub Yii2 fontawesome and successfully installed via composer. My problem is the code they given to get the specific icon to my SideNavbar is not working for me.
I have used FA::icon('home') inside my SideNav menu but this code out put only "> this. not the home icon.
code which i was used in my sidenav menu.
<?= SideNav::widget([
'type' => SideNav::TYPE_DEFAULT,
'heading' => 'System Functions',
'items' => [
[
'url' => '../dashboard/manager',
'label' => Yii::t('app','Dashboard'),
'icon' => 'home',
'active' => ($currentpage == 'Manager')
],
[
'url' => '#',
'label' => 'Purchase',
'icon' => FA::icon('home'),
'items' => [
[
'url' => '../dashboard/suppliers',
'label' => Yii::t('app','Suppliers'),
'icon'=>'transport',
'active' => ($currentpage == 'Suppliers')
],
[
'url' => '../dashboard/leaf-entry',
'label' => 'Leaf Collection',
'icon'=>'leaf',
'active' => ($currentpage == 'Leaf')
],
[
'url' => '../dashboard/payments',
'label' => 'Payments',
'icon'=>'phone',
'active'=> ($currentpage == 'Payments')
],
['label' => 'Reports', 'icon'=>'phone', 'url'=>'#']
],
],
[
'label' => 'Stock',
'icon' => 'question-sign',
'items' => [
['label' => 'Live Stock', 'icon'=>'info-sign', 'url'=>'#'],
['label' => 'Auction Despatch', 'icon'=>'phone', 'url'=>'#'],
['label' => 'Production Tracker', 'icon'=>'phone', 'url'=>'#'],
['label' => 'Reports', 'icon'=>'phone', 'url'=>'#']
],
],
[
'label' => 'Human Resource',
'icon' => 'question-sign',
'items' => [
['label' => 'Employees', 'icon'=>'info-sign', 'url'=>'#'],
['label' => 'Time Tracker', 'icon'=>'phone', 'url'=>'#'],
['label' => 'Payments', 'icon'=>'phone', 'url'=>'#'],
['label' => 'Reports', 'icon'=>'phone', 'url'=>'#']
],
],
[
'label' => 'End of day calculations',
'icon' => 'question-sign',
'items' => [
['label' => 'Water Basis', 'icon'=>'info-sign', 'url'=>'#'],
['label' => 'Dry Basis', 'icon'=>'phone', 'url'=>'#'],
['label' => 'Out Turn', 'icon'=>'phone', 'url'=>'#'],
['label' => 'Refuced Tea', 'icon'=>'phone', 'url'=>'#']
],
],
],
]);
?>
Output
And I also have used the Chrome DevTools to inspect the element, which gives me the below result. Hope this would be the problem.
You are using SideNav::widget, which uses the default icon prefix as "glyphicon glyphicon-". This is a SideNav property. If you are creating a SideNav using a glyphicon icon, then it will only use glyphicon icons. If you want to use Font Awesome icons, you need to change the icon prefix like this:
echo SideNav::widget([
'type' => SideNav::TYPE_DEFAULT,
'heading' => 'System Functions',
'iconPrefix' => ' ',
'items' => [
[
'url' => '#',
'label' => 'Home',
'icon' => 'fa fa-home',
]
]
]);
You can use only one type of icons at a time.
How to link from frontend page to backend in Yii2 using advanced template.... I want to link the login button from frondend to backend...when I click on login button in frontend it can redirect to login page in backend that I already created...
<div class="wrap">
<?php
NavBar::begin([
'brandLabel' => 'My Company',
'brandUrl' => Yii::$app->homeUrl,
'options' => [
'class' => 'navbar-inverse navbar-fixed-top',
],
]);
$menuItems = [
['label' => 'Home', 'url' => ['/site/index']],
['label' => 'About', 'url' => ['/site/about']],
['label' => 'Contact', 'url' => ['/site/contact']],
];
if (Yii::$app->user->isGuest) {
$menuItems[] = ['label' => 'Signup', 'url' => ['/site/signup']];
$menuItems[] = ['label' => 'Login', 'url' => ['/site/login']];
} else {
$menuItems[] = [
'label' => 'Logout (' . Yii::$app->user->identity->username . ')',
'url' => ['/site/logout'],
'linkOptions' => ['data-method' => 'post']
];
}
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => $menuItems,
]);
NavBar::end();
?>
<div class="container">
<?= Breadcrumbs::widget([
'links' => isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : [],
]) ?>
<?= Alert::widget() ?>
<?= $content ?>
</div>
</div>
`
You can try a relative path
if (Yii::$app->user->isGuest) {
$menuItems[] = ['label' => 'Signup', 'url' => ['/site/signup']];
$menuItems[] = ['label' =>'Login','url' =>'../../../backend/site/login'];
} else {
add this in common/config/main.php
under the section components
'components' => [
.....
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => false,
'showScriptName' => true,
],
.......
add to section components after urlManager in frontend/config/main.php
'urlManagerBackend' => [
'class' => 'yii\web\UrlManager',
'baseUrl' => 'http://backend.site.local',
'hostInfo' => 'http://backend.site.local',
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
],
in frontend/views/layout
add something like this
$menuItems = [
['label' => 'Home', 'url' => ['/site/index']],
['label' => 'Admin panel', 'url' => Yii::$app->urlManagerBackend->createUrl([''])],
];
This is my code:
<?=
Nav::widget([
'options' => ['class' =>'nav-pills nav-stacked'],
'encodeLabels' => false,
'items' => [
[
'label' => 'Blog',
'url' => ['/blog'],
'active' => \Yii::$app->controller->id == 'blog'
],
],
]);
?>
But I want this menu is active not only when the controller is "blog", but also when it is "category" and "post".
Change your code like below:
Nav::widget([
'options' => ['class' =>'nav-pills nav-stacked'],
'encodeLabels' => false,
'items' => [
[
'label' => 'Blog',
'url' => ['/blog'],
'active' => in_array(\Yii::$app->controller->id,['blog','category','post'])
],
],
]);
By above code, your menu item will be considered as active if controller id was one of ['blog','category','post'] values. The only change was:
'active' => in_array(\Yii::$app->controller->id,['blog','category','post'])