How to add a placeholder to a dropdown in Yii2? - yii2

I need to add a placeholder into my dropdown and somehow it doesn't work.
Here is my input field:
<?= $form->field($category, 'id')->dropDownList($categoryList, [
'id' => 'category-id',
'prompt' => 'Select category', [
'disabled' => true,
]
]); ?>
Could someone explain me what I'm doing wrong? Thanks for any help!

DropDownList has no placeholder, use 'prompt' instead:
$form->field($category, 'id')->dropDownList($categoryList, [
'id' => 'category-id',
'prompt'=>'- Select category -'
]);

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 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.

Set Dropdown list width in yii2

How to set Dropdown list width in Yii2, here the code
<?= $form->field($model, 'id_ts')
->dropDownList(
ArrayHelper::map(vision::find()->all(),'id_ts', 'vision'),
[
'prompt' =>'',
'disabled' => $model->isNewRecord ? true : false
]
) ?>
How to make dropdown list box wider ?
You can just add style in dropDownList show below code
<?= $form
->field($model, 'id_ts')
->dropDownList(
ArrayHelper::map(vision::find()->all(),'id_ts', 'vision'),
[
'style' => 'width:15px !important',
'prompt' => '',
'disabled' => $model->isNewRecord ? true : false
]
) ?>

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.

can use yii2 DepDrop::widget without model?

all developer kartik\DepDrop widget The ability to use without model
My code this but error
use kartik\widgets\DepDrop;
<?php echo DepDrop::widget([
'options' => ['id'=>'customer-city'],
'pluginOptions' => [
'depends => ['province'],
'placeholder => 'select ...',
'url' => Url::to(['/site/city'])
]
]); ?>
I use this code for show dropdown without lable
Yes, for it you need set the attribute name:
<?php echo DepDrop::widget([
'name' => 'city',
'options' => ['id'=>'customer-city'],
'pluginOptions' => [
'depends' => ['province'],
'placeholder' => 'select ...',
'url' => Url::to(['/site/city'])
]
]); ?>