Php Yii2 GridView搜索相关下拉列表

Php Yii2 GridView搜索相关下拉列表,php,yii2,yii2-advanced-app,yii2-validation,Php,Yii2,Yii2 Advanced App,Yii2 Validation,我正在使用Yi2 gridview加载国家、州和城市。我使用下拉菜单设置了国家、州、城市的搜索选项。如何在过滤器中创建相关下拉列表 <?= GridView::widget([ 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [ [ 'attribute' => 'country_id',

我正在使用Yi2 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);
          }
        ],

]); ?>

由于网格视图中的筛选表单更改,因此在表单更改时发送请求。。 您可以根据需要轻松创建过滤器列表

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

]); ?>

城市模型也一样,因为网格视图中的过滤器表单会在表单更改时更改发送请求。。 您可以根据需要轻松创建过滤器列表

<?= 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是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
);

$this->registerJs(如果$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
);

$this->registerJs(这里是依赖于国家/州/城市的下拉功能,其中城市是多选的

这里是观景台

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

这里是国家/州/城市相关的下拉功能,其中城市是多选的

这里是观景台

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

你可以做得越来越简单。例如,我有带眼镜的剧院,所以我的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']),
        ]...

你可以做得越来越简单。例如,我有带眼镜的剧院,所以我的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']),
        ]...

'filter'=>State::State($searchModel->country),在这一行,我得到了一个错误,比如未定义的变量:searchModel…我如何修复我在过滤器中使用的tisI,比如,,,,'filter'=>ArrayHelper::map(State::find()->where(['country\u id'=>searchModel->->country\u id])->all(),'id','name'),谢谢你的回答'filter'=>State::State($searchModel->country),在这一行中,我得到了一个错误,比如未定义的变量:searchModel…我如何修复我在过滤器中使用的tisI,比如,,,'filter'=>ArrayHelper::map(State::find()->where(['country\u id'=>$searchModel->country\u id])->all(),'id','name'),谢谢你的回答