2015-11-25 71 views
0

我使用Yii2 gridview來加載國家,州,城市。我使用下拉菜單爲國家,州,城市設置了搜索選項。如何在濾鏡中創建相關下拉列表?Yii2 GridView搜索相關下拉菜單

<?= GridView::widget([ 
    'dataProvider' => $dataProvider, 
    'filterModel' => $searchModel, 
    'columns' => [ 
     [ 
     'attribute' => 'country_id', 
     'label' => 'Country', 
     'filter' => Country::country(), 
     'value' => function($data){ 
     return Country::countryname($data->country_id); 
     } 
     ], 
     [ 

      'attribute' => 'state_id', 
      'filter' => State::state(), 
      'value' => function($data){ 
      return State::statename($data->state_id); 
      } 
     ], 
     [ 

      'attribute' => 'city_id', 
      'filter' => City::city(), 
      'value' => function($data){ 
      return City::cityname($data->city_id); 
      } 
     ], 

]); ?> 

回答

4

由於網格視圖改變過濾表形式發送請求的onChange .. 您可以輕鬆創建一個篩選器列表,

<?= GridView::widget([ 
    'dataProvider' => $dataProvider, 
    'filterModel' => $searchModel, 
    'columns' => [ 
     [ 
     'attribute' => 'country_id', 
     'label' => 'Country', 
     'filter' => Country::country(), 
     'value' => function($data){ 
     return Country::countryname($data->country_id); 
     } 
     ], 
     [ 

      'attribute' => 'state_id', 
      'filter' => State::state($searchModel->country), //Country Id/name from SearchModel --- you can add condition if isset($searchModel->country) then show else [] blank array 
      'value' => function($data){ 
      return State::statename($data->state_id); 
      } 
     ], 
     [ 

      'attribute' => 'city_id', 
      'filter' => City::city($searchModel->state), //Country Id/name from SearchModel --- you can add condition if 
      'value' => function($data){ 
      return City::cityname($data->city_id); 
      } 
     ], 

]); ?> 

正如你在狀態的過濾器中設置自己的函數/城市,,,取的$searchModel->state

設定值不變的代表數組列表,在國家示範性

class State extends \yii\db\ActiveRecord{ 

    public static state($country){ 
    if($country){ 
     return ['state']; 
    }else{ 
     return []; 
    } 
    } 
} 

只爲城市模型相同

+0

'過濾器'=>國家::狀態($ searchModel->國家),在此行中我得到了像未定義變量錯誤:searchModel ...我怎樣才能解決這個問題 – shanu

+0

我在過濾器中使用像,,,'filter'=> ArrayHelper :: map(State :: find() - > where(['country_id'=> $ searchModel-> country_id] ) - > all(),'id','name'), – shanu

+0

Now itz working – shanu

0

如果$ searchModel是ModelSearch的對象,在視圖中:

$this->registerJs(<<< EOT_JS 

     $('#ModelSearch[state_id]').on('change', function(ev) { 
      $.get(
       '<url>', 
       { parameters } 
       function(data) { 
         // if data is an html response... 
         $('#ModelSearch[city_id]').html(data); 
       } 
      ); 
     } 

EOT_JS 
); 
+0

It does not working – shanu

0

這裏是國家省份城市依賴下拉功能,其中城市爲多choosen

Here is View

  <?php 
      echo $form->field($model, 'country_id')->dropDownList(
       $con, 
       [ 
        'prompt'=>'Select Country', 
        'labal'=>false, 
        'onchange'=>' 
         $.get("'.Url::toRoute('/site/lists').'", { id: $(this).val() }) 
          .done(function(data) { 
           $("#'.Html::getInputId($model, 'state_id').'").html(data); 
          } 
         ); 
        '  
       ] 
     )->label('Country Name'); 



     // echo $form->field($model, 'state_id')->dropDownList(['prompt'=>'Select State']); 



      echo $form->field($model, 'state_id')->dropDownList(
       $state, 
       [ 
        'prompt'=>'Select State',      
        'onchange'=>' 
         $.get("'.Url::toRoute('/site/citi').'", { id: $(this).val() }) 
          .done(function(data) { 
           $("#'.Html::getInputId($model, 'citi_id').'").html(data); 

          } 
         ); 
        '  
       ] 
     )->label('State Name'); 



       echo $form->field($model, 'citi_id')->dropDownList(
       $state, 
       [ 
        'prompt'=>'Select City', 
        'multiple' => true, 
        'onchange'=>' 
         $.get("'.Url::toRoute('/site/citi').'", { id: $(this).val() }) 
          .done(function(data) { 
           $("#'.Html::getInputId($model, 'citi_id').'").html(data); 

          } 
         ); 
        '  
       ] 
     )->label('City Name'); 

      // echo $form->field($model, 'citi_id')->dropDownList(['prompt'=>'Select City']); 
      ?> 

這裏是Sitecontroller功能

國家相關國家

public function actionLists($id) 
{ 
    $model = new State(); 
    $rows = $model::find()->where(['country_id' => $id])->all();   
    $countPosts = $model::find(); 
    echo "<option>Select State</option>"; 

    if(count($rows)>0){ 
     foreach($rows as $row){ 
      echo "<option value='$row->id'>$row->name</option>"; 
     } 
    } 
    else{ 
     echo "<option>Select State</option>"; 
    } 
}  

**City Related to State** 

    public function actionCiti($id) 

{ 
     //die('hello'); 
    $model = new Citi(); 
    $rows = $model::find()->where(['state_id' => $id])->all();   
    $countPosts = $model::find(); 
    echo "<option>Choose City</option>"; 

    if(count($rows)>0){ 
     foreach($rows as $row){ 
      echo "<option value='$row->id'>$row->name</option>"; 
     } 
    } 
    else{ 
     echo "<option>No City Available</option>"; 
    } 
}