tonybolzan / yii-select2

Wrapper Widget to use jQuery Select2 in Yii application.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

select2 ajax calling

amujagtap21 opened this issue · comments

<?php 
$homeurl= Yii::$app->getUrlManager()->getBaseUrl();
$url = \yii\helpers\Url::to(['getartistlist']);
$initScript = <<< SCRIPT
  function (element, callback) {
    var id=\$(element).val();
    if (id!=="") {
       // alert('id not null' + id);
      \$.ajax("{$url}&id=" + id, {
        dataType: "json"
      }).done(function(data) { callback(data.results);});
    }
  }
SCRIPT;
            ?>
            <?= select2::widget([
          'name'=>'aaa',
          'pluginOptions'=>[
          'escapeMarkup' => new JsExpression("function(m) { return m; }"),
            'ajax' => [
              'url' => $url,
              'dataType' => 'json',
              'data' => new JsExpression('function(term,page) { return {search:term}; }'),
            'results' => new JsExpression('function(data,page) { return {results:data.results};}')
            ],
            'initSelection' => new JsExpression($initScript)

          ]
    ]); ?>

in controller

public function actionGetartistlist($q = null, $id = null) {
    $out = ['results' => ['id' => '', 'text' => '']];
    if (!is_null($q)) {
     $sql="SELECT ctid,ctname AS text FROM City_Master city where ctname LIKE '".$q."%'";
    $recc =Yii::$app->db->createCommand($sql)->queryAll();
    $out['results'] = array_values($recc);
    }
    elseif ($id > 0) {
        $out['results'] = ['id' => $id, 'text' => City_Master::find($id)->ctname];
    }
    return json::encode($out);
    }

but it not showing anything in dropdownlist plz help me to solve this problem

This Select2 Plugins is for Yii 1.x, I never tested this in Yii 2.
But his response appears to be wrong, $out variable dont have totals attribute.
A tip: Review your SQL because it is vulnerable to injection. (use bindValue() for $q)

thanx select2 plugin is also work for yii2. it works but now problem is...it show data in dropdown list but it not allowed me to select option from list