1

我正在使用 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

4 回答 4

4

由于网格视图中的过滤器表单更改发送请求 onChange of form.. 您可以轻松地创建一个过滤器列表为

<?= 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);
          }
        ],

]); ?>

正如您在 state/city 的过滤器中提供了自己的函数一样,获取代表设置值的数组列表$searchModel->state

状态模型

class State extends \yii\db\ActiveRecord{

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

城市模型也一样

于 2015-11-25T08:25:41.323 回答
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
);
于 2015-11-25T08:27:48.853 回答
0

你可以做得越来越简单。例如,我有带眼镜的剧院,所以我的 gridview 是:

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        'id',
        [
            'attribute' => 'theatre_id',
            'value' => 'theatre.title',
            'header' => '',
            'filter' => Html::activeDropDownList($searchModel, 'theatre_id', ArrayHelper::map(Theatre::find()->asArray()->all(), 'id', 'title'),['class'=>'form-control','prompt' => 'Select theatre']),
        ],
        [
            'attribute' => 'spectacle_id',
            'value' => 'spectacle.title',
            'header' => '',
            'filter' => Html::activeDropDownList(
                $searchModel,
                'spectacle_id',
                ((empty($searchModel->theatre_id))?
                    ArrayHelper::map(Spectacle::find()->asArray()->all(), 'id', 'title'):
                    ArrayHelper::map(Spectacle::find()->where(['theatre_id' => $searchModel->theatre_id])->asArray()->all(), 'id', 'title')),
                ['class'=>'form-control','prompt' => 'Select spectacle']),
        ]...

当我选择剧院时,我只有这个剧院的眼镜下拉菜单。当没有选择剧院时,我有来自所有剧院的所有可能的奇观。

于 2019-09-11T10:41:08.167 回答
0

这是多个选择城市的国家/地区城市相关下拉功能

这是视图

            <?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']);
            ?>

这是站点控制器功能

与国家相关的国家

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>";
    }
}
于 2016-04-21T11:00:37.507 回答