- Widget based on selectize.js extension http://brianreavis.github.io/selectize.js/
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist yii2mod/selectize "*"
or add
"yii2mod/selectize": "*"
to the require section of your composer.json.
Once the extension is installed, simply add widget to your page as follows:
- Tagging input:
echo $form->field($model, "attribute")->widget(Selectize::className(), [
'pluginOptions' => [
'persist' => false,
'createOnBlur' => true,
'create' => true
]
]);
- Select input:
echo $form->field($model, "attribute")->widget(Selectize::className(), [
'items' => [
'Yes',
'No'
],
'pluginOptions' => [
'persist' => false,
'createOnBlur' => true,
'create' => true
]
]);
- Tagging input with remote source and default values(If you want render select input, just setup items property):
- Setup view file:
// setup the following to get the existing data from database
$model->attribute = 'first, last';
// or if the data is an array you can preselect the tags like this
$model->attribute = implode(', ', ["first", "last"]);
echo $form->field($model, "attribute")->widget(Selectize::className(), [
'url' => '/site/search',
'pluginOptions' => [
'valueField' => 'name',
'labelField' => 'name',
'searchField' => ['name'],
'persist' => false,
'createOnBlur' => true,
'create' => true
]
]);
- Your action must return data in json format, example:
public function actionSearch($query = null)
{
Yii::$app->response->format = Response::FORMAT_JSON;
return [
['name' => 'Search Item 1'],
['name' => 'Search Item 2'],
];
}
You can find them on the options page