我正试图从Yii 1.1切换到Yii2。这是TextAreaJuiAutoComplete小部件的源属性
'source'=>"js:function(request, response) {
$.getJSON('".$url"', {
term: extractLast(request.term)
}, response);
}",
这在Yii2中不再使用yii \ jui \ AutoComplete了。任何人都可以给我一个暗示是什么原因?底层JavaScript对象应该是相同的。
如果我将以下代码设置为可行,但我想改为使用ajax调用。
'source' => [ "c++", "java", "php", "coldfusion", "javascript", "asp", "ruby" ],
答案 0 :(得分:3)
试试这个:
use yii\web\JsExpression;
.....
.....
'source'=>new JsExpression("function(request, response) {
$.getJSON('".$url."', {
term: request.term
}, response);
}"),
答案 1 :(得分:0)
用同样的考试。 控制器:
public function actionSearch($term)
{
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$rs = Cure::find()->where(['like', 'name_uz', $term])->all();
if($rs !=null){
$row_set = [];
foreach ($rs as $row)
{
$row_set[] = $row->name_uz; //build an array
}
return $row_set;
}else{
false;
}
}
在视图中:
<? use yii\jui\AutoComplete;?>
<?= AutoComplete::widget([
'model' => $model,
'attribute' => 'country',
'options' => ['class' => 'form-control'],
'clientOptions' => [
'source' => Url::to(['cure/search']),
'minLength'=>'2',
],
]); ?>