kartik-v / yii2-builder

Build forms (single or tabular) easily for Yii Framework 2.0.
http://demos.krajee.com/builder
Other
100 stars 50 forks source link

how to in select2 set default value yii2? #123

Closed Global90 closed 5 years ago

Global90 commented 7 years ago

how to in select2 set default value yii2

i use Select2 for Tag.

in insert new post and save tag with Select2 not problem but i need in edit post show Tags List with Select2.

<div class="col-xs-12">
            <?php
            echo Select2::widget([
                'model' => $modelKani,
                'name' => 'Kani',
                'id' => 'Kani',
                'data' => $data,
                'showToggleAll' => false,
                'options' => [
                    'placeholder' => 'insert Tag',
                    'multiple' => true,
                    'allowClear' => true,
                    'minimumInputLength' => 2,
                ],
            ]);
            ?>
        </div>
--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/45689021-how-to-in-select2-set-default-value-yii2?utm_campaign=plugin&utm_content=tracker%2F776156&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F776156&utm_medium=issues&utm_source=github).
santhoshvr commented 5 years ago

I fix this solution by using jquery trigger function. In below method we can set default value in kartik select2 widget. $('#id').val(11).trigger('change'); //in val() mention the default value of your select2 hash list

foxm4ster commented 5 years ago

I have changed value in options and it worked for me. For example: <?php echo Select2::widget([ 'model' => $modelKani, 'name' => 'Kani', 'id' => 'Kani', 'data' => $data, 'showToggleAll' => false, 'options' => [ 'value' => 'your default value', 'placeholder' => 'insert Tag', 'multiple' => true, 'allowClear' => true, 'minimumInputLength' => 2, ], ]); ?>

stale[bot] commented 5 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.