Select2初始值不会出现

时间:2017-03-20 02:41:18

标签: php yii2

如果很快就存在,我想在$ _GET上选择价值基础。

以下是我在视图中的代码

<?php
      /*if ($model->isNewRecord && !isset($model->agent_id_upper)) {
         $model->agent_id_upper = 65;
       } not working too*/ 

        //$model->agent_id_upper = 65; ->not working
        echo Select2::widget([
            'model' => $model,
            'name' => 'TbAgent[agent_id_upper]',
            //'id' => 'to_id',
            'initValueText' => '', // this only for ajax
            'data' => $data,
            'options' => [
            'placeholder' => 'Choose Agent ...',
            'multiple' => false,
            //'selected' => 65, -> not working
            //'value' => 65, -> not working
            'class' => ''
            ],
            ]
);
?>

65是记录的ID,如果选中则应显示ID的用户名。

但我的问题是没有选择65的用户名,它只显示占位符。

我如何解决这个问题?请给我参考。

已经读过这个

yii2 select2 by kartik-v set default value

Yii2: Kartik Select2: Initial Value from Model Attribute

http://www.yiiframework.com/forum/index.php/topic/52278-kartik-select2-not-select-corretly/

但上面没有运气。

提前致谢。

1 个答案:

答案 0 :(得分:0)

你试过这个吗?值不在选项内

<?= Select2::widget([
    'model' => $model,
    'name' => 'id',
    'data' => $data,
    'value' => 65,
    'options' => [
        'placeholder' => 'Choose Agent ...',
        'multiple' => false,
    ]
]) ?>