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
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With