【发布时间】:2015-12-15 18:17:24
【问题描述】:
下面是两个 Yii2.0 Autocomplete 小部件的 sn-ps
<?php
echo $form->field($model, 'countryId')->begin();
echo Html::activeLabel($model, 'countryId', ["class"=>"control-label col-md-4"]); ?>
<div class="col-md-5">
<?php
$data = Country::find()->select('countryName as label, id as id')->asArray()->all();
echo AutoComplete::widget([
'name' => 'countryId',
'clientOptions' => [
'source' => $data,
'autoFill'=>true,
'minLength'=>'1',
'select'=>"js:function(event,item){\$(\"#countryid\").val(item[1]);})",
],
'options' => [
'class' => 'form-control',
],
]);
?>
<?php echo Html::activeHiddenInput($model, 'countryId'); ?>
<?php echo Html::error($model, 'countryId', ['class'=>'help-block']); ?>
</div>
<?php echo $form->field($model, 'countryId')->end();?>
对于状态:
<?php
echo $form->field($model, 'stateId')->begin();
echo Html::activeLabel($model, 'stateId', ["class"=>"control-label col-md-4"]); ?>
<div class="col-md-5">
<?php
echo AutoComplete::widget([
'name' => 'countryId',
'clientOptions' => [
'source' => 'js:function(request, response) {
\$.getJSON(\"'+Yii::$app->urlManager->createUrl("site/get-states")+'\", { country: \$(\"#countryid\").val() },
response);
}',
'autoFill'=>true,
'minLength'=>'1',
'select'=>"js:function(event,item){\$(\"#stateid\").val(item[1]);}",
],
'options' => [
'class' => 'form-control',
],
]);
echo Html::activeHiddenInput($model, 'stateId');
echo Html::error($model, 'stateId', ['class'=>'help-block']); ?>
</div>
<?php echo $form->field($model, 'stateId')->end();?>
我的 getStates 控制器是:
public function actionGetStates(){
if(Yii::$app->request->isAjax && isset($_GET['term']) && isset($_GET['country'])) {
/* term is the default GET variable name that is used by
/ the autocomplete widget to pass in user input
*/
// \Yii::$app->response->format =
$name = $_GET['term'];
$country = $_GET['country'];
// this was set with the "max" attribute of the CAutoComplete widget
$limit = min($_GET['limit'], 50);
$statesArray = State::find()->select('stateName as label, id as id')->where('stateName LIKE :sterm AND countryId=:countryId')->params([':sterm'=>"%$name%", ':countryId'=> $country])->all();
return $statesArray;
}
}
这给我一个错误:
TypeError: this.source 不是函数
this.source( { term: value }, this._response() );
请帮我将国家 ID 发送给我的控制器。我也尝试过 extraParams 选项,但 jquery ui 不再支持该选项。
【问题讨论】:
-
这可能会有所帮助 - stackoverflow.com/questions/3308935/…
标签: php jquery autocomplete yii2