Yii2 tabs active not working - yii2

I have a tab widget like below.
<?php
echo \yii\bootstrap\Tabs::widget([
'items' => [
[
'label' => 'Administrators',
'url' => Url::to(['/admin/admin']),
],
[
'label' => 'Members Tracker',
'url' => Url::to(['/admin/members']),
],
[
'label' => 'Pages',
'url' => Url::to(['/admin/pages']),
],
],
]);
?>
Tabs are appearing correctly. But the active is not working for other tabs. Only active appears to first tab. If we click on other tabs('member Tracker','Pages') it routes correctly and can view the page contents. But tab1('Administrators") is in open state and other tabs in closed state. How to make a tab active when we click on it and others closed.Please help me.

In Yii2 Bootstrap Tabs Widget you can use active option.
For example in AdminController set $action to current action and pass to the view file:
<?= \yii\bootstrap\Tabs::widget([
'items' => [
[
'label' => 'Administrators',
'url' => Url::to(['/admin/admin']),
'active' => $action == 'admin',
],
[
'label' => 'Members Tracker',
'url' => Url::to(['/admin/members']),
'active' => $action == 'members',
],
[
'label' => 'Pages',
'url' => Url::to(['/admin/pages']),
'active' => $action == 'pages',
],
],
]); ?>

What i understand is that when you click on any of the tabs your page redirects, and you donot want the page to reload or redirect when you click on any of the tabs.
If that is correct then this is because you are using the url option and it takes a full route and redirects to that url, to use basic tab functionality you have to use the content option to display the text for the desired tab or you can load the view too see below
<?php
echo \yii\bootstrap\Tabs::widget([
'items' => [
[
'label' => 'Administrators',
//using a view file
'content' => $this->render('some-view'),
],
[
'label' => 'Members Tracker',
//using static content
'content' => 'This is some content for Member Tracker Tab'
],
[
'label' => 'Pages',
'content' => 'This is some content for Pages Tab'
],
],
])
?>

Related

Wrong rendering search field kartik select2

I use the select2 widget from kartik:
echo $form->field($model, 'person_ids')->widget(Select2::classname(), [
'data' => [1 => 'test1', 2 => 'test2'],
'theme' => Select2::THEME_KRAJEE_BS5,
'hideSearch' => true,
'options' => [
'multiple' => true
],
'pluginOptions' => [
'allowClear' => true
],
])->label('Personal');
The rendering of the search field is wrong - it intends the selected options. Any ideas how to fix this?

How to use selected value in Html::dropDownList with optgroups in Yii2?

in Yii2 I’m trying to choose a specific option with an Html::dropDownList that uses optgroup. I’m able to create the dropdown html just fine using a nested array but how do I select one of the choices? What do I use as the $selection value?
Here is the line of code that I'm currently using. If leave the selection blank, it all works fine, but as soon as I try to select a specific value, I can't get it to work.
echo Html::dropDownList(
'criteria',
['Page 1']['107141'],
[
'Page 1' => [
'107145' => 'Q1: some text',
'107141' => 'Q9: some text',
'107142' => 'Q10: some text',
'107143' => 'Q11: some text',
'107164' => 'Q14: some text',
],
'Page 2' => [
'107195' => 'some text',
],
],
[
'prompt' => [
'text' => 'Choose criteria',
'options' => []
],
'id' => 'criteriaSelector',
'class' => 'criteriaSelector',
'required' => 'required',
]
);
Here is the Yii documentation for the dropdownlist but I can't figure out what to do next:
https://www.yiiframework.com/doc/api/2.0/yii-helpers-basehtml#dropDownList()-detail
Thank you!
For some reason I kept thinking that the the selection needed to be an array element but, of course, since the dropdown option name has to be unique, the selection only needs to be a string. Here is the solution:
echo Html::dropDownList(
'criteria',
'107141',
[
'Page 1' => [
'107145' => 'Q1: some text',
'107141' => 'Q9: some text',
'107142' => 'Q10: some text',
'107143' => 'Q11: some text',
'107164' => 'Q14: some text',
],
'Page 2' => [
'107195' => 'some text',
],
],
[
'prompt' => [
'text' => 'Choose criteria',
'options' => []
],
'id' => 'criteriaSelector',
'class' => 'criteriaSelector',
'required' => 'required',
]
);

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

Edit content of export menu in yii2

I use export menu on my yii2 project. I download it from demos.krajee.com. I have been success to use it, but I want to edit content like in pdf, so it have title/header or footer or page number. How to make it?
There are at least two ways to do that.
For example you can set it as default setting in
Yii::$container->set('kartik\grid\GridView', [
'summary' => "Showing {begin} - {end} from {totalCount} records",
'captionOptions' => ['style' => 'font-size:0em;'],
'panel' => [
'heading' => false,
'before' => '{summary}',
'after' => '{pager}',
'footer' => false,
],
'hover' => true,
'toolbar' => [
'{export}',
'{toggleData}',
],
'export' =>[
'showConfirmAlert' => false,
],
'exportConfig' => [
kartik\grid\GridView::PDF => [
'label' => 'Save as PDF',
'config' => [
'methods' => [
'SetHeader' => ['Header'],
'SetFooter' => ['Footer whatever: ' . strftime("%c") . '||Page {PAGENO}'],
]
],
],
],
...
Or pass the settings individually to each widget in every view.
You can use methods in wrapper http://demos.krajee.com/mpdf#settings (look for methods) or directly from mpdf class if you need something more complicated.

Yii2: Menu active in different controllers

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