2017-05-09 78 views
0

通過在Yii2中使用kartik select2插件,我嘗試使國家和州的依賴關係下拉。對於國家Yii2 yi2中的Ajax請求

表單域,並指出:

$url = yii\helpers\Url::toRoute('op-client/lists'); 

$this->registerJs($this->render('script.js'), \yii\web\VIEW::POS_READY); 


$form->field($model, 'country_id')->widget(Select2::classname(), [ 
            'data' => $countryData, 
            'language' => 'en', 
            'options' => ['placeholder' => 'Select'], 
            'pluginOptions' => [ 
             'allowClear' => true, 
            ], 
            'pluginEvents' => 
            [ 
             'change' => 'function() 
              { 
               getstate 
               (
               $("#select2-opclient-country_id-container").val(), 
               "'.$url.'" 
              ) 
              }', 
            ], 
           ]).' 


           $form->field($model, 'states_id')->widget(Select2::classname(), [ 
            'data' => $statesData, 
            'language' => 'en', 
            'options' => ['placeholder' => 'Select'], 
            'pluginOptions' => [ 
             'allowClear' => true, 
            ], 

           ]).' 

的script.js

function getstate($countryid,url) 
{ 
    //console.log(startdate + enddate); 
var csrfToken = $('meta[name="csrf-token"]').attr("content"); 

    $.ajax({ 
     type:"POST", 
     cache:false, 
     url:url, 
     data:{countryid:countryid, _crsf:csrfToken}, 
     success:function(data){ 
      $("#select2-opclient-states_id-container").val(data); 
     }, 
    }) 
} 

控制器:

public function actionLists() 
    { 
     $request = Yii::$app->request; 

     $country = $request->post('countryid'); 

     $countStates = OpStates::find() 
         ->where(['country_id' => $country]) 
         ->count(); 

     $states = OpStates::find() 
         ->where(['country_id' =>$country]) 
         ->all(); 

     if($countStates > 0) 
     { 
      foreach($states as $state){ 
       echo "<option value='".$state->id."'>".$state->state_name."</option>"; 
      } 
     } 
     else 
     { 
      echo "<option></option>"; 
     } 
    } 

當我運行程序時,它顯示錯誤「未捕獲的ReferenceError :countryid未定義「。 但我想我已經把countryid放入了它?我在哪裏做錯了?

任何幫助/意見將不勝感激。謝謝

+0

'... getstate($ countryid,..'和'... countryid:countryid,..'在函數params中有一個** $ **! – leninhasda

回答

1

請檢查下面的代碼,我認爲你在country_id變量名中沒有犯什麼錯誤。

public function actionLists() 
    { 
     $request = Yii::$app->request; 

     $country = $request->post('country_id'); 

     $countStates = OpStates::find() 
         ->where(['country_id' => $country]) 
         ->count(); 

     $states = OpStates::find() 
         ->where(['country_id' =>$country]) 
         ->all(); 

     if($countStates > 0) 
     { 
      foreach($states as $state){ 
       echo "<option value='".$state->id."'>".$state->state_name."</option>"; 
      } 
     } 
     else 
     { 
      echo "<option></option>"; 
     } 
    } 

這裏

function getstate(countryid,url) 
{ 
    //console.log(startdate + enddate); 
var csrfToken = $('meta[name="csrf-token"]').attr("content"); 

    $.ajax({ 
     type:"POST", 
     cache:false, 
     url:url, 
     data:{countryid:countryid, _crsf:csrfToken}, 
     success:function(data){ 
      $("#select2-opclient-states_id-container").val(data); 
     }, 
    }) 
} 

這將解決您的問題。

+0

Ya我也發現了我的錯誤。 – ron