Yii2. Kartik Select2 widget width - yii2

My Select2 control is inside column with bootstrap class 'col-sm-3'. Also it is attached to form field. But width does not want to be 100% of parent column. If I make browser window smaller (after columns auto-rearangment) Select2 follows width of the parent column. Also if I specify width in px it changes the width. What could be a problem? Same story for form->field->textInput. Also I found the field width does not follow parent column width if the form is inline.
<?= $form->field($model, 'categories')->widget(Select2::className(), [
'data' => Category::availableCategories(),
'model' => $model,
'attribute' => 'categories',
'theme' => Select2::THEME_KRAJEE,
'size' => Select2::MEDIUM,
'showToggleAll' => false,
'language' => 'en',
'options' => ['placeholder' => 'Select a category...', 'id' => 'myselect'],
'pluginOptions' => [
'allowClear' => true,
'multiple' => true,
'width' => '200px',
],
])
?>

I had inline layout in my form. This was a problem.

Related

How to reset kartik depdrop to initial state with initial data

i have a depdrop in my project like
<?= $form->field($model, 'neighborhood_id')->widget(Select2::classname(), [
'data' => ArrayHelper::map($query,'id','placeWithCity'),
'options' => ['placeholder' => 'Select Your Neighborhood','id'=>'select_place'],
])->label(false); ?>
And
<?=$form->field($model, 'building_id')->widget(DepDrop::classname(), [
'data' =>ArrayHelper::map(Buildings::find()->all(),'id','buildingWithPlace'),
'options' => ['placeholder' => 'Select The Building','id'=>'select_building'],
'type' => DepDrop::TYPE_SELECT2,
'pluginOptions' => [
'depends' => ['select_place'],
'url'=>Url::to(['property-commercial-rent/buildings']),
'loadingText' => 'Loading buildings ...',
]
])->label(false);?>
The second dropdown is depend on the first one.Initially they both have full list of data without any filtering.When i select a neighborhood in the first dropdown then the second populates with the building names under that neighborhood. Then i have a reset button like and has an action like
$( "#reset-location" ).click(function() {
$(select_place).val('').trigger('change');
$(select_building).val('').trigger('change');
});
And this click resetting the both select.BUt the problem is the building has only the items under the previously select neighborhood.I want the make it all like initial stage.How can i do this
You don't need a reset button. On the neighbourhood field allow clear, so it would be like this
<?= $form->field($model, 'neighborhood_id')->widget(Select2::classname(), [
'data' => ArrayHelper::map($query,'id','placeWithCity'),
'options' => ['placeholder' => 'Select Your Neighborhood','id'=>'select_place'],
'pluginOptions' => [
'allowClear' => true
],
])->label(false); ?>
Then there will be a x in the field to clear it.

Yii2 logout Method Not Allowed (#405)

I'm using adminlte advanced template for backend. I want to add logout in the left column.
I've read other posts and I understand I've to add data method post. I've added it in following line in left.php file, but it doesn't work. How to make it work?
<?= dmstr\widgets\Menu::widget(
[
'options' => ['class' => 'sidebar-menu tree', 'data-widget'=> 'tree'],
'items' => [
['label' => 'Logout', 'icon' => 'file-code-o', 'url' => ['/site/logout'], 'data-method'=>'post'],
]
) ?>
It is extending the yii\widgets\Menu and you need to specify the template to modify or add any attribute to the link as the data-method="post" needs to be added to your link you should change the code to the following
echo
dmstr\widgets\Menu::widget(
[
'options' => ['class' => 'sidebar-menu tree', 'data-widget'=> 'tree'],
'items' => [
['label' => 'Logout', 'icon' => 'file-code-o', 'url' => ['/site/logout'], 'template'=>'{label}'],
]
);
You can add a form in to your click field:
$items[] = [
[
'label' => 'Logout',
'icon' => 'file-code-o',
'url' => ['/site/logout'],
'template' => Html::beginForm(array('site/logout')) .
Html::submitButton('Logout') . Html::endForm(),
],
];

Yii2 Gridview Custome column not display sort

I custome Gridview Column so sort not display
[
'attribute' => 'name',
'format' => 'raw',
'header' => $type == 1 ? 'Tên khách hàng' : 'Tên liên hệ',
'value' => function ($model) {
return Html::a($model->name, ['update', 'id' => $model->id], ['class' => 'alink']);
},
],
and sort not display
So how customer header and add sort?
i comment attribute header so it work.
i change header by label attribute so it work perfect.

Yii 2 Editable, how to use editable inside form?

$form = ActiveForm::begin();
..
echo Editable::widget([ //this break outter form, because this generate another form
'name'=>'person_name',
'asPopover' => true,
'value' => 'Kartik Visweswaran',
'header' => 'Name',
'size'=>'md',
'options' => ['class'=>'form-control', 'placeholder'=>'Enter person name...']
]);
ActiveForm::end();
So, I tried,
echo Form::widget([
'model'=>$model,
'form'=>$form,
'columns'=>1,
'attributes'=>[
'title'=>[
'label'=>false,
'type' => Editable::INPUT_TEXT,
'widgetClass' => Editable::className(),
'options' => [
'asPopover' => true,
]
],
]
]);
but, it shows input box all the time, not editable text.
how can I use editable widget inside form? without breaking outter form?
You can try this way:
<?= $form->field($model, 'person_name')->Editable::widget([
'name'=>'person_name',
'asPopover' => true,
'value' => 'value',
'header' => 'Name',
'size'=>'md',
'options' => ['class'=>'form-control', 'placeholder'=>'Enter person name...']
]);
?>
Note : Not tested yet.

Yii2 - NavBar widget hyperlink to div

anyone knows how can i in Yii2 using the navbar widget make a hyperlink to a div that is above the 100% height screen and is hidden in order that the screen scrolls down to it?
The code i'm using for the navbar is the standard one that comes with yii in layout.php
['label' => 'Contact', 'url' => ['/site/contact']],
I substitute 'url' => ['/site/contact']], --> by something like 'url' => ['#divid']],
But it doesn't works.
Many thanks in advance for an answer.
Just specify url as a string:
['label' => 'Contact', 'url' => '#divId'],
Url parameter is processed by Url::to() method, you can see there how string is interpreted:
a normal string: it will be returned as is.
Thank you very much for your help, it works. I was trying to do this
<body data-spy="scroll" data-target="#navbar" data-offset="50">
<?php $this->beginBody() ?>
<div class="wrap">
<?php
NavBar::begin([
//'brandLabel' => Yii::$app->name,
'brandLabel' => '<img src="SLOGAN.png" style="display:inline; vertical-align:top; margin-top:20px; margin-left: 10px;"/>',
'brandUrl' => Yii::$app->homeUrl,
'options' => [
'class' => 'navbar navbar-fixed-top navbar-inverse',
'id'=>'navbar'
],
]);
echo Nav::widget([
'options' => ['class' => 'nav navbar-nav navbar-left'],
'items' => [
['label' => 'HOME', 'url' => '#section1'],
['label' => 'ABOUT', 'url' => '#section2'],
['label' => 'SERVICES', 'url' =>'#section3'],
['label' => 'FAQS', 'url' => '#section4'],
['label' => 'CONTACTS', 'url' => '#section5'],