yiiyii2jquery-select2jquery-select2-4ui-select2

how to set default item in Select2 on Yii2


how to set default item in Select2 on Yii2

The initSelection method of Select2 3.5.x plugin is obsolete/removed. New initValueText property is been provided with the Select2 widget to cater to this (e.g. for ajax based loading).

but initValueText dont work!!!

<div class="col-xs-12">
            <?php
            $categories = [5 => 'test1', 7=> 'test2', 8=> 'test3'];
            echo Select2::widget([
                'initValueText' => $categories,
                'model' => $modelKani,
                'name' => 'Kani',
                'id' => 'Kani',
                'data' => $data,
                'showToggleAll' => false,
                'options' => [
                    'placeholder' => 'Insert Item',
                    'multiple' => true,
                    'allowClear' => true,
                    'minimumInputLength' => 2,
                    'dir' => 'rtl'
                ],
            ]);
            ?>
    </div>

result $data:

Array
(
    [5] = test1
    [7] = test2
    [8] = test3
)

Solution

  • work with this:

    'value' => [5, 10]
    

    item id 5,10 from $data

    Doc:

    http://demos.krajee.com/widget-details/select2#usage-tags