Yii2: Menu active in different controllers - yii2

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'])

Related

Yii2 gridview buttons problem (possibly after pjax)

<?= GridView::widget([
'id' => 'CompanyGrid',
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'floatHeader'=>true,
'floatOverflowContainer'=>true,
'floatHeaderOptions'=>['top'=>'0'],
'pjax'=>true,
'pjaxSettings' => [
'options' => [
'enablePushState' => false,
'enableReplaceState' => true,
]
],
'hover'=>true,
'toolbar' => [
'{export}',
'{toggleData}'
],
'panel' => [
'heading'=>'<h3 class="panel-title"><i class="glyphicon glyphicon-globe"></i> Companies</h3>',
'type'=>'secondary',
'before'=>Html::button('Create Company', ['value'=>Url::to('index.php?r=Company/company/create'), 'title' => 'Create Company', 'class' => 'btn btn-success', 'id' => 'modalButton']),
'after'=>false,
],
'columns' => [
['class' => 'kartik\grid\SerialColumn'],
'CompanyID',
'CompanyName',
['class' => 'kartik\grid\ActionColumn',
'template' => '{view} {update} {delete}',
'buttons' => [
'view' => function($url, $model){
return Html::a('<span class="fa fa-eye"></span>', ['view', 'id' => $model->CompanyID], [
'class' => 'activity-view-link',
'data-pjax'=>'w0',
'title' => Yii::t('yii', 'View Company: '.$model->CompanyID),
'data-toggle' => 'modal',
'data-target' => '#modal',
]);
},
'update' => function($url, $model){
return Html::a('<span class="fa fa-edit"></span>', ['update', 'id' => $model->CompanyID], [
'class' => 'activity-view-link',
'title' => Yii::t('yii', 'Edit Company: '.$model->CompanyID),
'data-toggle' => 'modal',
'data-target' => '#modal',
]);
},
'delete' => function($url, $model){
return Html::a('<span class="fa fa-trash"></span>', ['delete', 'id' => $model->CompanyID], [
'class' => '',
'data' => [
'confirm' => 'Are you absolutely sure? This action is not reversible',
'method' => 'post',
],
]);
}
],
],
],
]); ?>
In Controller
public function actionView($id)
{
$model = Company::findOne($id);
return $this->renderAjax('view', [
'model' => $model,
]);
}
I am new in yii2-advance-apps and i am trying to solve this problem. Already worked on this problem and cannot find any solution for this.
The problem is when I click on the gridview actions buttons, it works. However, after PJax, the button just don't work anymore.
However, if i reload the page, the button works again.
How to solve this problem. Thank you in advance.
'data-pjax' => 0, in action buttons options and not 'w0'

How to make a placeholder in a listBox

It is necessary that in the field listBoxt, there was a placeholder, something like "Select a role for the user", i.e. message that the user sees without selecting anything in the listbox. But this should include the option 'prompt' => 'remove the role' so that the user can remove the role through the drop-down list.
Is it possible to do this through the standard Yii functional without resorting to JS?
echo $form->field($model, 'additionalRoles', [
'options' => [
'class' => 'form-group',
],
])->listBox($additionalRoles, [
'class' => 'form-control j-multi-select2',
'prompt' => 'Select additional role',
]);
For example: https://jsfiddle.net/8e7avn2d/1/
<?=
$form->field($model, 'additionalRoles', [
'options' => [
'class' => 'form-group',
],
])
->listBox($additionalRoles, [
'class' => 'form-control j-multi-select2',
'prompt' => [
'text' => "Select additional role",
'options' => [
'disabled' => true,
'selected' => true,
'hidden' => true,
]]
]);
?>
to achieve your example you should use dropDownList instead of listBox
echo $form->field($model, 'additionalRoles', [
'options' => [
'class' => 'form-group',
],
])->dropDownList($additionalRoles, [
'class' => 'form-control j-multi-select2',
'prompt' => 'Select additional role',
]);

Add Custom Button in Kartik Yii2 Kartik DetailView

I already search and try every forum and example.
I am using the Kartik\DetailViwew in Yii2 and I can´t manage to put a single custom button in the buttons1 option.
The code I am working on:
echo DetailView::widget([
'model' => $model,
'attributes' => $attributes,
'mode'=>Yii::$app->request->get('edit')=='t' ? DetailView::MODE_EDIT : DetailView::MODE_VIEW,
'panel'=>[
'heading'=>$this->title,
'type'=>DetailView::TYPE_INFO,
],
'buttons1' => '{update}',
'bordered' => 'true',
'striped' => $striped,
'condensed' => $condensed,
'responsive' => $responsive,
'hover' => $hover,
'hAlign'=>$hAlign,
'vAlign'=>$vAlign,
'fadeDelay'=>$fadeDelay,
'deleteOptions'=>[ // your ajax delete parameters
'params' => ['id' => $model->p_id, 'custom_param' => true],
'url'=>['delete', 'id' => $model->p_id],
]
]);
In the
'buttons1' => '{update}',
According to the example http://demos.krajee.com/detail-view, there is a way to customize.
But there is no example. And the documentation not explain how to do this.
Can anyone help?
After research and some tryings, I did it by this way, you have to enable the edit mode (to show buttons), and modify the defatul template ({buttons}{title}), as this template is replaced if the template exists in vender grideview constants, just inject your code to add any button u want. Is not the rigth solution, but is a workaround that works well
<?php
$button1 = Html::a('<i class="glyphicon glyphicon-trash"></i>', Url::to(['delete', 'id' => $model->id]), [
'title' => 'Eliminar',
'class' => 'pull-right detail-button',
'data' => [
'confirm' => '¿Realmente deseas eliminar este elemento?',
'method' => 'post',
]
]);
$button2 = Html::a('<i class="glyphicon glyphicon-pencil"></i>', Url::to(['actualizar', 'id' => $model->id]), [
'title' => 'Actualizar',
'class' => 'pull-right detail-button',
]);
?>
<?=
DetailView::widget([
'model' => $model,
'hover' => true,
'hideAlerts' => true,
'enableEditMode' => true,
'mode' => DetailView::MODE_VIEW,
'hAlign' => 'left',
'panel' => [
'heading' => '&nbsp',
'type' => DetailView::TYPE_DEFAULT,
'headingOptions' => [
'template' => "$button1 $button2 {title}"
]
],
'attributes' => [
'id',
'identificacion',
'nombre',
],
])
?>
Try it like this:
'buttons1' => Html::a('<i class="glyphicon glyphicon-trash"></i>', Url::to(['delete', 'id' => $model->id]), [
'title' => 'Eliminar',
'class' => 'pull-right detail-button',
'data' => [
'confirm' => '¿Realmente deseas eliminar este elemento?',
'method' => 'post',
]
]),
If you have more than one button, try it like this:
$button1 = Html::a('<i class="glyphicon glyphicon-trash"></i>', Url::to(['delete', 'id' => $model->id]), [
'title' => 'Eliminar',
'class' => 'pull-right detail-button',
'data' => [
'confirm' => '¿Realmente deseas eliminar este elemento?',
'method' => 'post',
]
]);
$button2 = Html::a('<i class="glyphicon glyphicon-pencil"></i>', Url::to(['actualizar', 'id' => $model->id]), [
'title' => 'Actualizar',
'class' => 'pull-right detail-button',
]);
'buttons1' => "{$button1} {$button2}",

Yii2 Custom filter Dropdown for gridview

I am trying to add a custom filter named Sort By Month in the grid view header here is an example of the previous version of the site which I am revamping see below image
I was looking into the layout option of the grid view and added a drop-down in the layout template before {items}
GridView::widget(
[
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'layout' => '{summary}{errors}' . \kartik\widgets\Select2::widget(
[
'model' => $searchModel,
'attribute' => 'filter_month',
'theme' => \kartik\widgets\Select2::THEME_DEFAULT,
'data' => $searchModel->getFilterMonths(),
'pluginEvents' => [
"select2:select" => 'function() { $("#w2").submit();}',
// 'select2:select'=> new \yii\web\JsExpression("function(){console.log('here')}"),
],
'options' => [
'placeholder' => '--Select Month--',
],
'pluginOptions' => [
'allowClear' => true,
'width' => '160px',
],
]
) . '{items}{pager}',
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'name',
'title',
'catalog',
'upc_code',
[
'attribute' => 'created_on',
'label' => 'Created On',
'filter' => \yii\jui\DatePicker::widget(['dateFormat' => 'yyyy-MM-dd', 'model' => $searchModel, 'attribute' => 'created_on']),
'format' => 'html',
],
[
'attribute' => 'status',
'label' => 'Status',
'format' => 'raw',
'value' => function ($data) {
switch ($data->status) {
case 0:
return "Being Edited (" . $data->created_on . ")";
break;
case 1:
return ($data->maxdate == '') ? 'Active' : 'Active';
break;
case 2:
return "Expired";
break;
}
},
],
['class' => 'yii\grid\ActionColumn'],
],
]
);
Now I want to submit the filters form when I select an option from the drop-down, but could not figure out how to attach the default filter submit event with the drop-down options so that it filters the results when I select any option in the drop-down.
For Custom filter you have used the perfect layout but you have to provide your custom field as a filterSelector.
"filterSelector" => "#". Html::getInputId($searchModel, 'AttributeName'),

Font Awesome use inside the Yii2 SideNavbar

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.