tonybolzan / yii-select2

Wrapper Widget to use jQuery Select2 in Yii application.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Ajax call issue

indicalabs opened this issue · comments

This is not a problem in this extension but I need help.

I have seen your extension and written extension for yii2 version.
That is working fine except ajax call
When I click in the textfield, Nothing returns and below is the java script error in the firbug console.
TypeError: data.call is not a function
(?)() select2.js (line 431)
data = data ? data.call(self, query.term, query.page, query.context) : null;

CONTROL IN THE VIEW:

field($model,'country_id')->widget(yii\select2\Select2::className(),[ 'model'=>$model, 'attribute'=>’country_id', // 'language' => 'ru', // 'multiple' => true, 'clientOptions' => [ // 'allowClear' => true, 'width' => 300, 'ajax' => [ 'url' => Yii::$app->urlManager->createAbsoluteUrl(‘data/country/findcountry') , 'dataType' => 'json', 'data' => 'js:function(term,page) { return {q: term, page: page, page_limit: 10}; }', 'results' => 'js:function(data,page) { return {results: data}; }', ], ], ]) ?>

Thanks in advance

Probably your problem is the line:

'data' => 'js:function(term,page) { 
    return {q: term, page: page, page_limit: 10}; 
}',

In my extension this line is evaluated by the functions CHtml::textField() and CHtml::dropDownList() calling the method clientChange().
If equal in your case, probably this line is not being evaluated correctly to javascript.
Look javascript generated code, if it is a string, it may be wrong, should be a function.

That did the trick. Thank you very much
In Yii2 we need to define javascript with yii\web\JsExpression to encode properly.