Skip to content
This repository has been archived by the owner on Feb 17, 2018. It is now read-only.

select2 ajax calling #7

Open
amujagtap21 opened this issue Jul 27, 2015 · 2 comments
Open

select2 ajax calling #7

amujagtap21 opened this issue Jul 27, 2015 · 2 comments

Comments

@amujagtap21
Copy link

<?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

@tonybolzan
Copy link
Owner

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)

@amujagtap21
Copy link
Author

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants