如何在 Yii2 的 Select2 中设置默认项
how to set default item in Select2 on Yii2
如何在Yii2的Select2中设置默认项
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).
但是 initValueText 不起作用!!!
<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>
结果$数据:
Array
(
[5] = test1
[7] = test2
[8] = test3
)
处理这个:
'value' => [5, 10]
来自 $data
的项目 ID 5,10
文档:
提供 model
和 attribute
或 name
和 value
。您提供了 model
和 name
,因此 model
被忽略,value
设置为 null
.
至于 initValueText
- 其描述为:"the displayed text in the dropdown for the initial value when you do not set or provide data
(e.g. using with ajax)"。您已提供 data
。
如何在Yii2的Select2中设置默认项
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).
但是 initValueText 不起作用!!!
<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>
结果$数据:
Array
(
[5] = test1
[7] = test2
[8] = test3
)
处理这个:
'value' => [5, 10]
来自 $data
的项目 ID 5,10文档:
提供 model
和 attribute
或 name
和 value
。您提供了 model
和 name
,因此 model
被忽略,value
设置为 null
.
至于 initValueText
- 其描述为:"the displayed text in the dropdown for the initial value when you do not set or provide data
(e.g. using with ajax)"。您已提供 data
。