Kartik Select2 load ajax when other input change value - php

I need to load data with ajax to a Kartik Select2 but only when certain input changes values. Kind of like...
<?= $form->field($model, 'id_list')->widget(Select2::classname(), [
'data' => [],
'theme' => Select2::THEME_BOOTSTRAP,
'options' => [
'placeholder' => 'List',
],
'pluginOptions' => [
'allowClear' => true,
'action' => '#input_first'.change // or something
'ajax' => [
'url' => Url::to(['/list']),
'dataType' => 'json',
'data' => new JsExpression('function(params) { return {q:params.term}; }')
],
],
]);?>

Have you tried to use Select2 with DepDrop plugin (http://demos.krajee.com/widget-details/depdrop)?

Related

How to use multiple select on yii kartik widget select2 and ajax

i'm trying to use the Kartik Select2 Widget for my Yii2 project.
I'll use the Ajax Loading and everithing works find, but if i set the multiple option it gives me this error.
PHP Warning – yii\base\ErrorException array_combine(): Both parameters
should have an equal number of elements
This error is in Select2.php on this line
$this->data = $multiple ? array_combine((array)$key, (array)$val) : [$key => $val];
i think it's because the data attribute is missing, but if i add data attribute ajax doesn't work properly.
This is my view:
$form->field($model, 'lista_art')->widget(Select2::classname(), [
'initValueText' => "", // set the initial display text
//'data' => '',
'options' => ['placeholder' => 'Select a color ...',
//'multiple' =>true, // error here
],
'pluginOptions' => [
'tags' => true,
'tokenSeparators' => [',', ' '],
'allowClear' => true,
'minimumInputLength' => 3,
'language' => [
'errorLoading' => new JsExpression("function () { return 'Waiting for results...'; }"),
],
'ajax' => [
'url' => \yii\helpers\Url::to(['lista-articoli']),
'dataType' => 'json',
'data' => new JsExpression('function(params) { return {q:params.term}; }')
],
'escapeMarkup' => new JsExpression('function (markup) { console.log(markup);return markup; }'),
'templateResult' => new JsExpression('function(lista_art) { return lista_art.art_modello; }'),
'templateSelection' => new JsExpression('function (lista_art) { return lista_art.art_modello; }'),
],
]);
Also tried to insert multiple option inside 'pluginOptions' array but doesnt work.
Is it possible to use multiple option with Ajax Loading?
P.s. I've check my developer toolbar and the response is correct, and gives me what i've expected.
You forgot to set data attribute
Sample how to use kartik select 2 with model and active form
$model->keywords = [1, 2]; // NOTE THIS IS AN ARRAY of keys
$form->field($model, 'keywords')->widget(Select2::className(), [
'pluginOptions' => [
'tags' => true,
'multiple' =>true,
],
'data' => [1=>'keyword1', 2=>'keyword2', 3=>'keyword3'],
])
Update
Try to set up data as empty array like
'data' => []

yii2 kartik input widget not working properly

I added buttons for delete and drag but not working, my code in the view:
<?= $form->field($model, 'imageFiles[]')->widget(FileInput::classname(), [
'options' => ['multiple' => true, 'accept' => 'image/*',
'id'=>'inputFile'],
'pluginOptions' => [
'rtl'=>'true',
'fileActionSettings'=>['showZoom'=>true, 'showRemove' =>true,
'showDrag'=>true],
'previewFileType' => 'image',
'maxFileCount' => 4,
'showUpload' => false,
]
]);
Where are i making mistakes?
When using FORM submission mode (without uploadUrl). in this scenario you cannot REMOVE preview thumbnails before they are uploaded - ONE by ONE (you can only clear all - this is a native HTML FILE input limitation as one cannot edit the files in the input). in this case we must add uploadUrl:
<?= $form->field($model, 'imageFiles[]')->widget(FileInput::classname(), [
'options' => ['multiple' => true, 'accept' => 'image/*',
'id'=>'inputFile'],
'pluginOptions' => [
'uploadUrl' => '/site/index',
'rtl'=>'true',
'fileActionSettings'=>['showZoom'=>true, 'showRemove' =>true,
'showDrag'=>true],
'previewFileType' => 'image',
'maxFileCount' => 4,
'showUpload' => false,
]
]);
This is ajax submission mode with uploadUrl.

Yii2 required validation rule on "select2" widget

I'm using kartik select2 widget in Yii2 framework. Required validation rule doesn't work on it.
Here is my view code:
$form->field($model, 'city')->widget(\kartik\select2\Select2::classname(), [
'data' => $cities,
'options' => [
'class' => 'form-control',
'placeholder' => 'Please select city...',
'multiple' => false,
],
'pluginOptions' => [
'allowClear' => true
],
])->label('City');
Here is my model rule code:
[['city'], 'required'],
[['city'], 'integer']
Any idea to make the dropdown required?
Use this in plugin option
pluginOptions' => [
'initialize' => true,
],
This is working code for me
$form->field($model, 'tech_type')->widget(Select2::classname(), [
'options'=>['id'=>'tech-id'],
'data' => ArrayHelper::map(Techtypes::find()->asArray()->all(), 'tech_id', 'tech_type'),
'pluginOptions'=>[
'initialize' => true,
'placeholder' => 'Select Technician Type ...',
]
]);

How do I can remove the label above input file form?

I've an application that was developed using Yii2, and this application I've use Kartik Input File for upload file.
Case
From the example above, I want to remove / hide the "File" label.
And I think, that label based on model name.
This is the code I use:
<?=
$form->field($model, 'file')->widget(FileInput::classname(), [
'options' => [
'accept' => 'doc/*', 'file/*',
'enableLabel' => false,
],
'pluginOptions' => [
'allowedFileExtensions' => ['csv', 'xls', 'xlsx'],
'showUpload' => FALSE,
'showPreview' => FALSE,
]
]);
?>
How do I can remove the label above?
Thanks
For removing the label you can simply use the following:
<?=
$form->field($model, 'file')->widget(FileInput::classname(), [
'options' => [
'accept' => 'doc/*', 'file/*',
'enableLabel' => false,
],
'pluginOptions' => [
'allowedFileExtensions' => ['csv', 'xls', 'xlsx'],
'showUpload' => FALSE,
'showPreview' => FALSE,
]
])->label(false);
?>

Yii2 kartik datepicker multiple inline

I want a Datepicker inline with multidate enabled.
It is rendered correctly but returns only the last selected date.
Return value: '12.11.2016'
If I set 'type' to TYPE_INPUT everything is working fine.
Expected return Value: '23.11.2016, 24.11.2016, 18.11.2016, 12.11.2016'
<?= $form->field($model, 'dateString')->widget(DatePicker::className(), [
'type' => DatePicker::TYPE_INLINE,
'pluginOptions' => [
'multidate' => true,
],
]);
?>
I'm using "kartik-v/yii2-widget-datepicker": "#dev" and the model is a ActiveRecord Model.
Seems to have been fixed with this commit: https://github.com/kartik-v/yii2-widget-datepicker/commit/39e0e71277d0f115341e118a2b879a0dfcbd01c3
This will solve the problem
echo $form->field($model, 'date_time')->widget(DatePicker::classname(), [
'options' => [
'value' => "Jul-11-2020,Jul-14-2021,Jul-12-2020",
'class' => "col-md-12 form-control picker"
],
'readonly' => false,
'language' => 'en',
'type' => DatePicker::TYPE_INLINE,
'pluginOptions' => [
'format' => 'M-dd-yyyy',
'todayHighlight' => true,
'multidate' => true,
],
]);

Categories