以下是两个Yii2.0自动完成小部件的片段
<?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不再支持该选项了。
答案 0 :(得分:1)
<强> Sulution 强> VIEW
<?php
//hidden input for COUNTRY ID
echo $form->field($model, 'COUNTRY_ID')->hiddenInput()
?>
<?php
// Input for COUNTRY NAME
echo AutoComplete::widget( [
'name' => 'COUNTRY',
'id' => 'COUNTRY',
'options' => [
'class' => 'form-control',
'placeholder' => 'Начните набирать название',
],
'clientOptions' => [
'source' => Url::to(['/libriary/autocomplete/country']),
'autoFill' => true,
'minLength' => '0',
// Get and set value to hidden field
'select' => new JsExpression("function( event, ui ) {
$('#". Html::getInputId($model, 'COUNTRY_ID')."').val(ui.item.value).trigger('change');
$('#COUNTRY').val(ui.item.label);
console.log($('#userdetails-country_id').val());
return false;
}")
],
]
);
?>
<?php
//hidden input for CITY ID
echo $form->field($model, 'CITY_ID')->hiddenInput()
?>
<?php echo AutoComplete::widget( [
'name' => 'CITY',
'id' => 'CITY',
'options' => [
'class' => 'form-control',
'placeholder' => 'Начните набирать название',
],
'clientOptions' => [
'source' =>new JsExpression('function(request, response) {
$.getJSON("'.Url::to(['/libriary/autocomplete/city']).'", {
term: $("#CITY").val(),
country: $("#userdetails-country_id").val()
}, response);
}'),
'autoFill' => true,
'minLength' => '0',
// Get and set value to hidden field
'select' => new JsExpression("function( event, ui ) {
$('#". Html::getInputId($model, 'CITY_ID')."').val(ui.item.value).trigger('change');
$('#CITY').val(ui.item.label);
return false;
}")
],
]
); ?>
控制器
public function actionCountry($term)
{
$data = [];
$countries = Country::find()->andFilterWhere(['like', 'country_name_ru', $term])->limit(10)->all();
if ($countries) foreach ($countries as $country) {
$data[] = [
'label' => $country->country_name_ru,
'value' => $country->id_country,
];
}
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
return $data;
}
public function actionCity($term,$country)
{
$data = [];
$cites = City::find()->where(['id_country'=>$country])->andFilterWhere(['like', 'city_name_ru', $term])->limit(20)->all();
if ($cites) foreach ($cites as $city) {
$data[] = [
'label' => $city->city_name_ru,
'value' => $city->id_city,
];
}
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
return $data;
}