Yii2 select2 by kartik-v set the default value

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

the widget is attached to the 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 the value in this field

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

What can I do?

UPDATE!

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

+4
source share
3 answers

Its a bit later .. but I also had the same problem .. I solved it by assigning a 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

            }",
],
   ]);
 ?>
+1
source

, #ajax, placeholder, initValueText

<?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...." , "" , -

+1

:

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

, templateResult '' templateSelection '.

JsExpression address.text( address.name) : .

PHP, :

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;
}

, - .

0

All Articles