The preferred way to install this extension is through composer.
Either run
composer require --prefer-dist yii2-extensions/select2:^0.1
or add
"yii2-extensions/select2": "^0.1"
to the require section of your composer.json
file.
use Yii2\Extension\Select2\Select2;
<?= $form->field($model, 'user_id')->widget(Select2::className(), [
'items' => ArrayHelper::map(User::find()->all(), 'id', function($model) {
return $model->username . " <{$model->email}>";
}),
'clientOptions' => [
'placeholder' => 'User',
'allowClear' => true,
],
'clientEvents' => [
'change'=>'function (e) {
console.log("Select2 change.");
}',
],
]) ?>
use Yii2\Extension\Select2\Select2;
<?= Select2::widget([
'name' => 'InputName',
'items' => ArrayHelper::map(User::find()->all(), 'id', function($model) {
return $model->fio . " <{$model->email}>";
}),
'options' => [
'multiple' => true,
]
'clientOptions' => [
'placeholder' => 'User',
'allowClear' => true,
]
]) ?>
For more details see: https://github.com/select2/select2
This code was initially developed by contributors at yii2-cms and hosted at repo: https://github.com/yii-cms/yii2-select2. We thank them all. For continuity and maintenance we have moved a copy here.
Check the documentation testing to learn about testing.
The MIT License. Please see License File for more information.