PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, March 19, 2022

[FIXED] how to set default item in Select2 on Yii2

 March 19, 2022     jquery-select2, jquery-select2-4, ui-select2, yii, yii2     No comments   

Issue

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



Answered By - user3770797
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing