Yii2:Select2,如何在Gridview或Tabularform中设置initValueText?

时间:2015-07-29 07:37:34

标签: yii2

我需要为Select2设置initValueText,它在循环中,如gridview或Tabularform。但我不知道如何为每个人设定正确的价值。

<?= TabularForm::widget([
    'dataProvider' => $dataProvider,
    'form' => $form,
    'actionColumn' => false,
    'checkboxColumn' => false,
    'attributeDefaults' => [
        'type' => TabularForm::INPUT_RAW,
    ],
    'attributes' => [
        'test' => [
            'type' => Form::INPUT_WIDGET,
            'widgetClass' => Select2::className(),
            'options' => [
                'name' => 'test',
                'options' => [
                    'class' => 'test-to-select',
                ],
                'pluginOptions' => [
                    'allowClear' => true,
                    'minimumResultsForSearch' => 'Infinity',
                    'ajax' => [
                        'url' => Url::to(['/test/get-list']),
                        'dataType' => 'json',
                        'data' => new JsExpression('function(term,page) {
                            return {term : term.term};
                        }'),
                        'results' => new JsExpression('function(data,page) {
                            return {results:data.results};
                        }'),
                        'cache' => true
                    ]
                ],
                'initValueText' => 'Selected Text' /// how can I set this in gridview or Tabularform?
            ],

        ],
    ]
]) ?>

当然这不起作用,

'initValueText' => function($model){
    retur $model->textValue;
}

任何帮助将不胜感激。

5 个答案:

答案 0 :(得分:1)

如果你想要动态的initValueText,进入tabularform,你可以用这种方式使用options闭包:

'test' => [
        'type' => Form::INPUT_WIDGET,
        'widgetClass' => Select2::className(),
        'options' => function($model, $key, $index, $widget) {
            $initValueText = empty($model['textValue']) ? '' : $model['textValue'];
            return [
                'name' => 'test',
                'options' => [
                    'class' => 'test-to-select',
                ],
                'initValueText' => $initValueText,
                'pluginOptions' => [
                    'allowClear' => true,
                    'minimumResultsForSearch' => 'Infinity',
                    'ajax' => [
                        'url' => Url::to(['/test/get-list']),
                        'dataType' => 'json',
                        'data' => new JsExpression('function(term,page) {
                            return {term : term.term};
                        }'),
                        'results' => new JsExpression('function(data,page) {
                            return {results:data.results};
                        }'),
                        'cache' => true
                    ]
                ],
            ];
        }
    ],

答案 1 :(得分:0)

例如,如果是city的属性,那么试试这个..

$cityDesc = empty($model->city) ? '' : City::findOne($model->city)->description;

'initValueText' => $cityDesc, // set the initial display text

对于初始值,首先为$ model属性赋值,如果不应该赋值,则此属性可以取值。

答案 2 :(得分:0)

使用包含您要显示的选项的数组设置data参数。例如,对于城市:

'options' => [
    'data' => \yii\helpers\ArrayHelper::map(\app\models\City::find()->orderBy('id')->asArray()->all(), 'id', 'name'),
]

答案 3 :(得分:0)

尝试将选项置于ouside

之内
'widgetClass' => Select2::className(),
'options' => [
    'initValueText' => 'Selected Text'

答案 4 :(得分:0)

如果不使用isset,则如果使用了一个过滤器,则会返回错误。

      [
            'attribute' => 'ad_partner_id',
            'value' => function ($model, $key, $index, $widget) {
                return $model->partner->name;
            },
            'filter' => Select2::widget([
                'model' => $searchModel,
                'initValueText' => !empty($searchModel->ad_partner_id) ? $searchModel->partner->name : "",
                'attribute' => 'ad_partner_id',
                'options' => ['placeholder' => Yii::t('app', 'Search  Partner ...')],
                'pluginOptions' => ['allowClear' => true, 'autocomplete' => true,
                    'ajax' => ['url' => Url::base() . '/partner/get-partners',
                        'dataType' => 'json',
                        'data' => new JsExpression('function(params) { return {q:params.term}; }'),
                    ],
                ],
            ]),
        ],