I have downloaded yii2-widgets-master and don't know where I should keep it.
I am facing the error Class kartik\widgets\SideNav not found.
My code is here
<?php
use yii\helpers\Url;
use kartik\widgets\SideNav;
?>
<div id = "links">
<div class="user-img"><i class="fa fa-user" aria- hidden="true"></i>
<div class="text-center info"> <p>Wecare</p></div>
</div>
<?php
$menuItems[] = ['label' => 'DFenX - Yii2 User - '. Yii::t('app','User Admin Panel'), 'icon' => 'cog', 'url'=>Url::to(['/user/admin/index'])];
$menuItems[] = ['label' => Yii::t('app','Authentication manager'), 'icon' => 'th-list', 'items' => [
['label' => 'Settings', 'icon' => 'th-list', 'items' => [
['label' => '/user/settings', 'url'=>Url::to(['/user/settings'])],
['label' => '/user/settings/profile', 'url'=>Url::to(['/user/settings/profile'])],
['label' => '/user/settings/account', 'url'=>Url::to(['user/settings/account'])],
['label' => '/user/settings/networks', 'url'=>Url::to(['/user/settings/networks'])],
]],
['label' => 'Registration', 'icon' => 'th-list', 'items' => [
['label' => '/user/registration/register', 'url'=>Url::to(['/user/registration/register'])],
['label' => '/user/registration/resend', 'url'=>Url::to(['/user/registration/resend'])],
]],
['label' => 'Security', 'icon' => 'th-list', 'items' => [
['label' => '/user/security/login', 'url'=>Url::to(['/user/security/login'])],
['label' => '/user/security/logout', 'url'=>Url::to(['/user/security/logout'])],
]],
['label' => 'Recovery', 'icon' => 'th-list', 'items' => [
['label' => '/user/recovery/request', 'url'=>Url::to(['/user/recovery/request'])],
['label' => '/user/recovery/reset', 'url'=>Url::to(['/user/recovery/reset'])],
]],
]];
$type = SideNav::TYPE_PRIMARY;
$heading = '<i class="glyphicon glyphicon-user"></i> ' . Yii::t('app','USER Admin - UTILITIES');
echo SideNav::widget([
'type' => $type,
'encodeLabels' => false,
'heading' => $heading,
'items' =>$menuItems,
]);
?>
</div>
Add to composer.json:
"kartik-v/yii2-widget-sidenav": "*"
Run composer update in console under project directory.
Related
I have a yii2 application, and this is a part of my layouts/main.php view:
<?php
/* #var $this \yii\web\View */
/* #var $content string */
...
<body>
<?php $this->beginBody() ?>
<div class="wrap">
<?php
NavBar::begin([
'brandLabel' => Yii::t('app', Yii::$app->name),
'brandUrl' => Yii::$app->homeUrl,
'options' => [
'class' => 'navbar-default navbar-fixed-top',
],
]);
// everyone can see Home page
$menuItems[] = ['label' => Yii::t('app', 'Home'), 'url' => ['/index.php/']];
// we do not need to display About and Contact pages to employee+ roles
if (!Yii::$app->user->can('employee')) {
//$menuItems[] = ['label' => Yii::t('app', 'About'), 'url' => ['/site/about']];
$menuItems[] = ['label' => Yii::t('app', 'Contact'), 'url' => ['/site/contact']];
}
// display Users to admin+ roles
if (Yii::$app->user->can('admin')){
$menuItems[] = ['label' => Yii::t('app', 'Users'), 'url' => ['/user/index']];
}
// display Logout to logged in users
if (!Yii::$app->user->isGuest) {
//add items, rooms etc. to menu for logged in users
$menuItems[] = ['label' => Yii::t('app', 'Personnel'), 'url' => ['/person/index']];
$menuItems[] = ['label' => Yii::t('app', 'Items'), 'url' => ['/item/index']];
$menuItems[] = ['label' => Yii::t('app', 'Locations'), 'url' => ['/location/index/']];
$menuItems[] = ['label' => Yii::t('app', 'Buildings'), 'url' => ['/building/index/']];
$menuItems[] = ['label' => Yii::t('app', 'Rooms'), 'url' => ['/room/index/']];
$menuItems[] = [
'label' => Yii::t('app', 'Logout'). ' (' . Yii::$app->user->identity->username . ')',
'url' => ['/site/logout'],
'linkOptions' => ['data-method' => 'post']
];
}
// display Signup and Login pages to guests of the site
if (Yii::$app->user->isGuest) {
$menuItems[] = ['label' => Yii::t('app', 'Signup'), 'url' => ['/site/signup']];
$menuItems[] = ['label' => Yii::t('app', 'Login'), 'url' => ['/site/login']];
}
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => $menuItems,
]);
NavBar::end();
?>
As you can see, I have location, room and building in my menu. However, I'd like to only have location in the main menu, and room and building in location's sub-menu. Is there a simple way to do this?
I don't know what exactly your sub-menu is, if you only want to generate a dropdown list, you may
// display Logout to logged in users
if (!Yii::$app->user->isGuest) {
//add items, rooms etc. to menu for logged in users
$menuItems[] = [
'label' => Yii::t('app', 'Locations'),
'url' => ['/location/index/'],
'items' => [
['label' => Yii::t('app', 'Personnel'), 'url' => ['/person/index']],
['label' => Yii::t('app', 'Items'), 'url' => ['/item/index']],
['label' => Yii::t('app', 'Buildings'), 'url' => ['/building/index/']],
['label' => Yii::t('app', 'Rooms'), 'url' => ['/room/index/']],
[
'label' => Yii::t('app', 'Logout'). ' (' . Yii::$app->user->identity->username . ')',
'url' => ['/site/logout'],
'linkOptions' => ['data-method' => 'post']
],
],
];
}
I have customized my gridview in Yii2 to show columns, headers, and pager in a certain way
'dataProvider' => $dataProvider,
//'filterModel' => $searchModel,
'filterSelector' => 'select[name="per-page"]',
'tableOptions'=> ['class'=>'table datatable-header-footer datatable-header-footer'],
'showFooter' => true,
'layout'=>"{items}\n\n{summary}\n\n<div class='text-right'>{pager}</div>\n",
//'summary' => "{begin} - {end} {count} {totalCount} {page} {pageCount}",
'summary' => " <br/> Affichage de {begin} à {end} des {totalCount} lignes <br/> <br/>",
'formatter' => ['class' => 'yii\i18n\Formatter','nullDisplay' => ' - '],
'pager' => [
'nextPageLabel' => '→',
'prevPageLabel' => '←',
'firstPageLabel' => true,
'maxButtonCount' => 5,
'lastPageLabel' => true
],
'columns' => [
// ['class' => 'yii\grid\SerialColumn'],
//'id', ...
['class' => 'yii\grid\ActionColumn',
'contentOptions' => ['style' => 'width:10%;'],
'header'=>'Actions',
'template' => '{all}',
'buttons' => [
'all' => function ($url, $model, $key) {
return ButtonDropdown::widget([
'encodeLabel' => false,
'label' => 'Choisir',
'dropdown' => [
'encodeLabels' => false,
'items' => [
[
'label' => \Yii::t('yii', '<i class="icon-search4"></i> Details'),
'url' => ['view', 'id' => $key],
],
[
'label' => \Yii::t('yii', '<i class="icon-pencil5"></i> Modifier'),
'url' => ['update', 'id' => $key],
'visible' => true,
],
[
'label' => \Yii::t('yii', '<i class="icon-list"></i> Annonces'),
'url' => ['annonces', 'agence_id' => $key],
'visible' => true,
],
[
'label' => \Yii::t('yii', '<i class="icon-list2"></i> Agents'),
'url' => ['professionnels', 'agence_id' =>$key],
'visible' => true,
],
[
'label' => \Yii::t('yii', '<i class="icon-bin"></i> Supprimer'),
'linkOptions' => [
'data' => [
'method' => 'post',
'confirm' => \Yii::t('yii', 'Are you sure you want to delete this item?'),
],
],
'url' => ['delete', 'id' => $key],
'visible' => true, // same as above
],
],
'options' => [
'class' => 'dropdown-menu-right', // right dropdown
],
],
'options' => [
'class' => 'btn-default',
'style' => 'padding-left: 5px; padding-right: 5px;', // btn-success, btn-info, et cetera
],
'split' => true,
]);
Im wondering if there is a way to create a gridview "style" so I can call and apply this style to every gridview in the project !
Yes, extend GridView class with your own, set all properties, override anything you need and use your class instead of default GridView.
i'm working with yii2 and uikit. i'm trying to build a navbar with the menu widget and i have this:
<nav class='uk-navbar'>
<?php
echo Menu::widget([
'activateItems' => true,
'activateParents' => true,
'activeCssClass' => 'uk-active',
'encodeLabels' => false,
'items' => [
['label' => 'Inicio', 'url' => ['site/index']],
['label' => 'Juridico', 'url' => ['juridico/index']],
['label' => 'Pagos', 'url' => ['pagos/index']],
['label' => 'Universidades', 'url' => ['universidades/index']],
['label' => 'Usuarios', 'url' => ['usuarios/index']],
['label' => 'Planes', 'options' => ['class' => 'uk-parent', 'data' => 'uk-dropdown'], 'url' => ['#'], 'items' => [
['label' => 'Planes Juridico', 'url' => ['#']],
['label' => 'Planes Universidades', 'url' => ['#']],
]],
['label' => 'Login', 'url' => ['site/login'], 'visible' => Yii::$app->user->isGuest],
],
'submenuTemplate' => '<div class="uk-dropdown uk-dropdown-navbar"><ul class="uk-nav uk-nav-navbar">{items}</ul></div>',
'options' => [ 'class' => 'uk-navbar-nav'],
]);
?>
</nav>
all i'm missing is this attribute but i don't know how to set it 'data-uk-dropdown'
i need this result in order to the dropdown to work:
<nav class="uk-navbar">
<ul class="uk-navbar-nav">
<li class="uk-active">Active</li>
<li>Item</li>
<li class="uk-parent" data-uk-dropdown>
Parent
<div class="uk-dropdown uk-dropdown-navbar">
<ul class="uk-nav uk-nav-navbar">
<li>Item</li>
<li>Another item</li>
<li class="uk-nav-header">Header</li>
<li>Item</li>
<li>Another item</li>
<li class="uk-nav-divider"></li>
<li>Separated item</li>
</ul>
</div>
</li>
</ul>
</nav>
Thanks in advance.
have you try to add in the 'options' like below?
<?php
echo Menu::widget([
'activateItems' => true,
'activateParents' => true,
'activeCssClass' => 'uk-active',
'encodeLabels' => false,
'items' => [
['label' => 'Inicio', 'url' => ['site/index']],
['label' => 'Juridico', 'url' => ['juridico/index']],
['label' => 'Pagos', 'url' => ['pagos/index']],
['label' => 'Universidades', 'url' => ['universidades/index']],
['label' => 'Usuarios', 'url' => ['usuarios/index']],
['label' => 'Planes', 'options' => ['class' => 'uk-parent', 'data' => 'uk-dropdown'], 'url' => ['#'], 'items' => [
['label' => 'Planes Juridico', 'url' => ['#']],
['label' => 'Planes Universidades', 'url' => ['#']],
]],
['label' => 'Login', 'url' => ['site/login'], 'visible' => Yii::$app->user->isGuest],
],
'submenuTemplate' => '<div class="uk-dropdown uk-dropdown-navbar"> <ul class="uk-nav uk-nav-navbar">{items}</ul></div>',
'options' => [
'class' => 'uk-navbar-nav',
'data-uk-dropdown' => ''
],
]);
?>
I think this is how you can add data attributes same method would work in other Yii2 places. So you specify the key 'data' and then add an array of just the attribute names (without prefixing them with 'data'. You can read more about it here.
NavBar::begin([
'options' => [
'id' => 'main-nav-bar',
'class' => 'navbar-inverse',
'data' => ['my-attribute' => 'myValue'] // This line specifically.
],
'innerContainerOptions' => ['class' => 'container-fluid']
]);
In my main.php, I have a menu:
'items' => [
['label' => 'Home', 'url' => ['/site/index']],
['label' => 'Biz Staff', 'url' => ['bizstaff/index'], 'visible' => User::isBizAdmin(), 'items' => [
['label' => 'Staff List', 'url' => 'index.php?r=user/index'],
['label' => 'Add Staff', 'url' => 'index.php?r=user/create'],
]],
['label' => 'Transaction', 'url' => ['transactions/index'], 'visible' => User::isBizAdmin() || User::isBizStaff(), 'items' => [
['label' => 'Transactions', 'url' => 'index.php?r=transactions/index'],
['label' => 'Add Transactions', 'url' => 'index.php?r=transactions/create'],
]],
Yii::$app->user->isGuest ?
['label' => 'Login', 'url' => ['/site/login']] :
['label' => 'Logout (' . Yii::$app->user->identity->username . ')',
'url' => ['/site/logout'],
'linkOptions' => ['data-method' => 'post']
],
],
Here, I am logged in as the superadmin (note that only Home and Logout menu is visible to superadmin user). Inside the homepage (Home menu) is a List of Biz Admins which is placed inside a GridView widget. It has an Action column where view, update and delete icons are placed. When I click the view icon of a specific Biz Admin, it will then render a detailed view page of that Biz Admin where its Store Name and List of Staff are seen. In this page, there is a View Store button which will redirect to the bizadmin view/page.
Whenever superadmin lands on bizadmin view/page, the menu bar should now change to:
Home, Biz Staff, Transaction, Logout
How do I do this? Is it set in the visible attribute?
Any of your answers would be highly appreciated. I am currently stuck in this problem.
Yes. You should use visible property. 'visible' => true or 'visible' => false.
Or you may assemble an array. Like that:
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();
See in Advanced template - https://github.com/yiisoft/yii2-app-advanced/blob/master/frontend/views/layouts/main.php
You can make use of Yii::$app->controller->action->id or Yii::$app->controller->id to render menus based on your path.
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->controller->action->id == 'put your id here') {
$menuItems[] = ['render your menu here'];
} else {
'do something here'
}
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => $menuItems,
]);
NavBar::end();
I'm trying to put modal in a navbar in my yii2 project. I'm using yii2-bootstrap extension.
Code for my nav:
NavBar::begin([
'brandLabel' => 'My Company',
'brandUrl' => Yii::$app->homeUrl,
'options' => [
'class' => 'navbar-inverse navbar-fixed-top',
],
]);
$menuItems = [
['label' => 'Home', 'url' => ['/site/index']],
//['label' => 'facilities', 'url' => ['/facilities/index']],
['label' => 'Hotel',
'items' => [
['label' => 'Facilities', 'url' => ['/facilities/index']],
// '<li class="divider"></li>',
// '<li class="dropdown-header">Dropdown Header</li>',
['label' => 'Cuisines', 'url' => ['/cuisines/index']],
],
]
];
if (Yii::$app->user->isGuest) {
$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();
?>
code for modal:
<?php
Modal::begin([
'header' => '<h2>Hello world</h2>',
'toggleButton' => ['label' => 'click me'],
]);
echo 'Say hello...';
Modal::end();
?>
can anyone please tell me how to add this modal to navbar?
First place the modal with an id on your site/index
<?php
use yii\bootstrap\Modal;
Modal::begin(['id' => 'modal',
'header' => '<h2>Hello world</h2>']);
echo "Say Hello...";
Modal::end();
?>
Then create a jQuery action in your controller/SiteController
function actionShowmodal(){
$js='$("#modal").modal("show")';
$this->getView()->registerJs($js);
return $this->render('index');
}
Finally in views\layouts\main add the link in your Nav::wigdet
['label' => 'Show Modal', 'url' => ['/site/showmodal']],
$menuItems = [
['label' => 'Home', 'url' => ['/site/index']],
//['label' => 'facilities', 'url' => ['/facilities/index']],
['label' => 'Hotel',
'items' => [
['label' => 'Facilities', 'url' => ['/facilities/index']],
// '<li class="divider"></li>',
// '<li class="dropdown-header">Dropdown Header</li>',
['label' => 'Cuisines', 'url' => ['/cuisines/index']],
// insert this line
'<li><a data-toggle="modal" data-target="#modal" style="cursor: pointer;">Click me gently!</a></li>',
],
]
];
And for the modal widget:
<?php
Modal::begin([
'header' => '<h2>Hello world</h2>',
'toggleButton' => ['label' => 'click me'],
'id' => 'modal', // <-- insert this modal's ID
]);
echo 'Say hello...';
Modal::end();
?>