【问题标题】:Yii2 Dependent Autocomplete send Extra ParametersYii2 依赖自动完成发送额外参数
【发布时间】: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 不再支持该选项。

【问题讨论】:

标签: php jquery autocomplete yii2


【解决方案1】:

解决方案 查看

<?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;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-17
    • 2022-07-07
    • 2012-11-25
    • 1970-01-01
    • 2017-11-24
    • 1970-01-01
    • 2014-07-10
    • 1970-01-01
    相关资源
    最近更新 更多