yii2 select2 by kartik-v set default value

假装没事ソ 提交于 2019-12-13 13:27:22

问题


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"; on the view the field remain blank

what can i do?

UPDATE!

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


回答1:


The problem was this:

  'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
  'templateResult' => new JsExpression('function(address) { return address.name; }'),
  'templateSelection' => new JsExpression('function (address) { return address.name; }'),

It seems that this plugin want a specific return for the keys 'templateResult' and 'templateSelection'.

I've modified the JsExpression return value with address.text (instead of adress.name) as the guide examples : Link.

And then i've modified also the PHP method that return the results:

public function actionAddresses($q = NULL, $id = NULL)
{

    Yii::$app->response->format = Response::FORMAT_JSON;

    $results = array();
    if (!is_null($q)) {

        $geocoder = new GeocodingClient();
        $request = $geocoder->lookup(['address' => $q]);
        $counter = 1;
        foreach ($request['results'] as $key => $value) {
            $results['results'][] = [
                'id' => $counter,
                'text' => $value['formatted_address'], // Instead of 'name'
                'coordinate' => $value['geometry']['location']
            ];
            $counter++;
        }
    }
    return $results;
}

I hope this may help someone with a similar problem.




回答2:


Its bit late.. but i too had same problem.. i resolved it by assigning value

  <?=
  $form->field($model, 'address')->widget(Select2::className(), [
                          'initValueText' => $model->address,
                           'value' => $model->address,
                           '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

            }",
],
   ]);
 ?>



回答3:


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; }'),
        ],
    ]);?>

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




回答4:


you use kartik select2, in controller you have set a value (id/ids) and name values for 'initValueText'

$model->id = 1; // or if arrays [1,3,4,5]
$username = User::find()->select('name')->where('id' => $model->id)->scalar();

and in view kartik select2

$form->field($model, 'username')->widget(Select2::className(), [
'initValueText' => $username;
...



回答5:


I fix this solution by using trigger function. In below method we can set default value in kartik select2 widget.

$('#id').val(11).trigger('change'); //in val() mention the hash value of your select2 list 


来源:https://stackoverflow.com/questions/35627490/yii2-select2-by-kartik-v-set-default-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!