Tuesday, April 24, 2018

Yii2 Select2 onchange

kartik Select2 onChange and Ajax Call

In this example you can see a function userAlert() and this will call after typing in select2 text input

In below example you can also see ajax calling. In the parameter set your URL.

You can also set your own label in this example.

echo $form->field($model, 'user')->widget(Select2::classname(), [
                                'options' => ['id' => 'user_id', 'class'=>'kartik2', 'title'=>'User', 'onchange' =>"userAlert();"],
                                'pluginOptions' => [
                                        'ajax' => [
                                        'url' => 'Your Url',
                                        'dataType' => 'json',
                                    ],
                                    'allowClear' => false,
                                    'minimumInputLength' => 3,
                                    'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
                                    'templateResult' => new JsExpression('function(user) { return user.text; }'),
                                    'templateSelection' => new JsExpression('function (user) { return user.text; }'),
                                ],
                            ])->label('Label you can set here');


1 comment:

  1. Really awesome blog. Your blog is really useful for me. Thanks for sharing this informative blog. Keep update your blog.
    PHP Yii Developers in India

    ReplyDelete

Recent Update

yii2 session handling

use yii\web\Session; $session = Yii::$app->session; // start session $session->open(); // close session $session->close(); ...

Most Search