V
V
Veronika Stepanovna2017-04-23 11:53:34
Yii
Veronika Stepanovna, 2017-04-23 11:53:34

How to customize yii2-selectize-widget widget?

I use a widget yii2-selectize-widgetto visually select publication tags. It is necessary for me that already selected tags are displayed in the widget field by default and also a drop-down list with available tags appears when clicking on the form. The form:

echo $form->field($model, 'tags')->widget(Selectize::className(), [
    'clientOptions' => [
        'labelField' => 'name',
        'valueField' => 'id',
        'plugins' => ['remove_button'],
        'persist' => true,
        'create' => false,
        'options' => ,
    ]
]);

An optionsarray of values ​​available for selection gets into. But how to make the necessary values ​​​​selected by default so that when editing an article, you do not select them every time anew?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2017-04-23
@sargss

I like demos.krajee.com/widget-details/select2 better based on https://select2.github.io, at least there are docks there.
And your task is solved somehow like this:

use kartik\widgets\Select2;
$data = [
    "red" => "red",
    "green" => "green",
    "blue" => "blue",
    "orange" => "orange",
];
 
// Tagging support Multiple
$model->colorTags =  ['red', 'green']; // initial value
echo $form->field($model, 'colorTags')->widget(Select2::classname(), [
    'data' => $data,
    'options' => ['placeholder' => 'Select a color ...', 'multiple' => true],
    'pluginOptions' => [
        'tags' => true,
        'tokenSeparators' => [',', ' '],
        'maximumInputLength' => 10
    ],
])->label('Tag Multiple');

Video about links, tags and select2 widget

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question