yii2 select 2 code example

Example 1: kartik select2

$form->field($model, 'attribute')->widget(\kartik\select2\Select2::classname(), [
                'data' => \yii\helpers\ArrayHelper::map(Model::find()->all(), 'id', 'username'),
                'options' => ['placeholder' => 'Tanlang', 'multiple' => false, 'required' => true],
                'theme' => \kartik\select2\Select2::THEME_KRAJEE,
                'size' => 'xs',

Example 2: select 2 yii2

use kartik\select2\Select2;

// Normal select with ActiveForm & model
echo $form->field($model, 'state_1')->widget(Select2::classname(), [
    'data' => $data,
    'language' => 'de',
    'options' => ['placeholder' => 'Select a state ...'],
    'pluginOptions' => [
        'allowClear' => true
    ],
]);

// Multiple select without model
echo Select2::widget([
    'name' => 'state_2',
    'value' => '',
    'data' => $data,
    'options' => ['multiple' => true, 'placeholder' => 'Select states ...']
]);

Example 3: select2 yii2

$ php composer.phar require kartik-v/yii2-widget-select2 "@dev"

Tags:

Php Example