0

I use Select2 widget in multiply mode in YII2 framework. "kartik-v/yii2-widget-select2": "@dev" - this one I have downloaded via composer.

kartik-v/yii2-widget-select2 dev-master dd09e46

I added initial values with ajax on widget init ('initSelection'). And added another ajax method to suggest new values on user's typing. When user select one from list, it replaced initial values what were added on init. New values replace initial values, but don't another new. I want new values to add to initial instead of replace it.

<?= $form->field($model, 'security[]')->widget(Select2::class, [

                    'attribute'     => 'security',
                    'hideSearch'    => true,
                    'data'=>$security_data,
                    'options'       => [
                        'placeholder' => 'Security',
                        'multiple'    => true,
                    ],
                    'pluginOptions' => [
                        'allowClear'         => true,
                        'minimumInputLength' => 1,
                        'ajax'               => [
                            'url'      => Url::toRoute([ '/admin/security/select-items' ]),
                            'dataType' => 'json',
                            'data'     => new JsExpression('function(params) { return {q:params.term}; }'),
                            'results' => new JsExpression('function(data,page) { return {results:data.results}; }'),
                        ],
                        'initSelection'      => new JsExpression('function(element, callback) { var id = '.Yii::$app->request->getQueryParams()['id'].';if(id !== "") {$.ajax("' . \yii\helpers\Url::toRoute([ '/admin/security/init-items' ]) . '", {data: {id: id},dataType: "json"}).done(function(data) {callback(data.results);});}}'),
                    ],
                ]); ?>

And here is my api methods:

 public function actionSelectItems($q = null){

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

    $out = ['results' => []];
   if(!empty($q)){
       $items = Security::find()->where(['like', 'title', $q])->all();
       foreach ($items as $item){
           $out['results'][] = ['id'=>$item->id, 'text'=>$item->title];

       }
   } 

    return $out;
}

public function actionInitItems($id = null){

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

    $adv = Adv::findOne($id);
    $security = @json_decode($adv->security, true);
    $out = ['results' => []];
    foreach ($security as $item){
        $text = Security::findOne($item)->title;
        $out['results'][] = ['id'=>$item, 'text'=>$text];
    }
    return $out;
}

Is there some sort of settings or I missed something when handle http result?

Valik Tralik
  • 69
  • 1
  • 12
  • are you saying the the saved options disappear when you hit the submit button of the form or when you try to select more options from the select2 ? – Muhammad Omer Aslam Oct 16 '19 at 20:18
  • Only init options disapear. It happens when I choose one more item from dropdown list, that suggested me backend after searching on substring – Valik Tralik Oct 17 '19 at 08:24
  • you should use the `initValueText` option to load the existing value labels, can you match your select with [this source](https://pastebin.com/0LJNTuQF) that i took from my existing project where i am using the same thing just change the models and names accordingly – Muhammad Omer Aslam Oct 17 '19 at 11:08
  • this example didn`t work for me. No values appear on init, when I put array in initValueText. Maybe we use different plugin version, because I found in docs that initValueText should be a string. – Valik Tralik Oct 18 '19 at 08:59
  • yes the init value should be a string and that is what the example is doing it is strange that it is not working for you what is the version of select2 you are using and yii2? try running `composer show -i` on the project root and add the version numbers so that we can match and find the problem – Muhammad Omer Aslam Oct 18 '19 at 09:58
  • i notice that you have referred to the js polugin repo in your question are you sure you are using `kartik\select2` ? – Muhammad Omer Aslam Oct 18 '19 at 09:59
  • I changed the refference in question. I was wrong, that was plugin for YII2 – Valik Tralik Oct 18 '19 at 15:12

0 Answers0