【问题标题】:Select2 initial value doesn't appearSelect2 初始值不出现
【发布时间】:2017-03-20 02:41:18
【问题描述】:

如果很快存在,我想根据 $_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/

但上面没有运气。

提前致谢。

【问题讨论】:

    标签: php yii2


    【解决方案1】:

    你试过吗?值不在选项内

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

    【讨论】:

      猜你喜欢
      • 2023-03-03
      • 2017-03-12
      • 2015-08-01
      • 2019-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-21
      相关资源
      最近更新 更多