Ajax Loading Select2

hi, iam trying to to use krajee Select ajax loading method but i am get following error …

Unknown Method – yii\base\UnknownMethodException

Calling unknown method: app\Controllers\ListingController::createUrl()

anyhelp ?

i know the error means there is no createUrl function in my controller… but on demo page also no create url function mentioned in controller…

http://demos.krajee.com/widget-details/select2#ajax

Thanks

1 Like

You should use.




\yii\helpers\Url::to(['/<your-controller>/<your-ajax-action>']);



The Select2 widget demo docs are updated for this change. The earlier yii release had the createUrl method in the controller (which is not valid anymore).

1 Like

thank you :D

but i cant select from my result dropdown list :blink:

1 Like

You may check the sample code for Select2 ajax loading in the demo and ensure you have setup your controller action and widget correctly.

If you still face an issue you can create an issue on github.

1 Like

Hi Karthik,

this is my code, did i made any mistake can u help me

view:




$url = \yii\helpers\Url::to(['/listing/citylist']);


    echo $form->field($model, 'stadt')->widget(Select2::classname(), [

        'options' => ['placeholder' => 'Search for a city ...'],

            'pluginOptions' => [

                'allowClear' => true,

                'minimumInputLength' => 2,

                'ajax' => [

                'url' => $url,

                'dataType' => 'json',

                'data' => new JsExpression('function(term,page) { return {search:term}; }'),

                'results' => new JsExpression('function(data,page) { return {results:data.results}; }'),

            ],

        ],

    ]);



controller:




   public function actionCitylist($search = null) {


    $out = ['more' => false];

        if (!is_null($search)) {


            $connection = \Yii::$app->db;

            $ortQuery = $connection->createCommand('SELECT DISTINCT ort AS text FROM plz_de WHERE ort LIKE "%' . $search .'%" LIMIT 20');


            $data = $ortQuery->queryAll();


            $out['results'] = array_values($data);

        }

        else {

            $out['results'] = ['ort' => '', 'text' => 'No matching records found'];

        }

        echo Json::encode($out);

    }



1 Like

Check exactly the code for view and controller in the Select2 ajax loading example.

For example, you seem to be using the fieldname ort instead of id. In addition, you do not have the fieldname text returned in your query output.

If you are changing these - you must change your javascript code as shown in the example as well to match these names.

Would request you to post an issue on github if you are still facing issues.

1 Like