我使用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);
}
],
]); ?>
答案 0 :(得分: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 [];
}
}
}
对于城市模型来说是一样的
答案 1 :(得分: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
);
答案 2 :(得分: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']);
?>
这是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>";
}
}
答案 3 :(得分: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']),
]...
当我选择剧院时,我只能从该剧院中挑选眼镜。如果未选择剧院,则所有剧院都有所有可能的眼镜。