yii2 select2 by kartik-v 设置默认值 [英] yii2 select2 by kartik-v set default value

查看:56
本文介绍了yii2 select2 by kartik-v 设置默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于 yii2 kartik-v widget select 2 的问题.

I've a question about yii2 kartik-v widget select 2.

小部件附加到我视图中的一个字段

the widget is attached to a field in my view

<?=
$form->field($model, 'address')->widget(Select2::className(), [
    'options' => ['placeholder' => 'Inserta an address '],
    'pluginOptions' => [
        'allowClear' => true,
        'minimumInputLength' => 3,
        'ajax' => [
            'url' => Url::to(['register/addresses']),
            'dataType' => 'json',
            'data' => new JsExpression('function(params) { return {q:params.term}; }')
        ],
        'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
        'templateResult' => new JsExpression('function(address) { return address.name; }'),
        'templateSelection' => new JsExpression('function (address) { return address.name; }'),
    ],
    'pluginEvents' => [
        "select2:select" => "function(e) {
                      // some function

                }",
    ],
]);
?>

如果在我的控制器中我想为此字段设置一个值

if in my controller i want to set to this field a value

like: $model->address = "Some Value"; 在视图中字段保持空白

like: $model->address = "Some Value"; on the view the field remain blank

我能做什么?

更新!

正如文档所说,如果我使用此插件的 ajax 版本,我可以使用选项:'initValueText'.所以我试图设置 'initValueText' =>$model->address, 但结果是一样的

As the documentation say i can use the option: 'initValueText' if i use the ajax version of this plugin. So i've tried to set 'initValueText' => $model->address, but the result is the same

推荐答案

嘿,我遇到了与 #ajax 相同的问题,既没有显示占位符,也没有显示 initValueText我做的是这个

hey i had the same issue with #ajax neither placeholder nor initValueText were showing what i did was this

<?php
    $product = $model->product_id ? Product::findOne($model->product_id)->name : 'select ....';
    echo $form->field($model, 'product_id')->widget(Select2::classname(), [
        'initValueText' => $product, // set the initial display text
        // 'options' => ['placeholder' => 'select ....'],
        'pluginOptions' => [
            'allowClear' => true,
            'minimumInputLength' => 3,
            'ajax' => [
                'url' => Url::to(['list']),
                'dataType' => 'json',
                'data' => new JsExpression('function(params) { return {q:params.term}; }')
            ],
            'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
            'templateResult' => new JsExpression('function(product_id) { return product_id.name; }'),
            'templateSelection' => new JsExpression('function (product_id) { return product_id.name; }'),
        ],
    ]);?>

如果型号是新的,则显示select ....",否则显示产品名称因为选项"也没有做太多希望这对某人有希望

if the model is new 'select ....' is shown else the product's name since "options" is not doing much either hope this will be hopeful to someone

这篇关于yii2 select2 by kartik-v 设置默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆