Yii2 GridView 搜索依赖下拉
Yii2 GridView search dependent dropdown
我正在使用 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);
}
],
]); ?>
由于网格视图中的过滤器表单更改发送请求 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的filter中提供了你自己的函数,取代表$searchModel->state
的设定值的数组列表
在状态模型中
class State extends \yii\db\ActiveRecord{
public static state($country){
if($country){
return ['state'];
}else{
return [];
}
}
}
城市模型也一样
如果 $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
);
这是国家/地区城市相关下拉功能,其中城市是多项选择
这是视图
<?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>";
}
}
你可以做得越来越简单。例如,我有带眼镜的剧院,所以我的 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']),
]...
当我选择剧院时,我只有这个剧院的眼镜下拉列表。不选剧场的时候,我有所有剧场所有可能的眼镜。
我正在使用 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);
}
],
]); ?>
由于网格视图中的过滤器表单更改发送请求 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的filter中提供了你自己的函数,取代表$searchModel->state
的设定值的数组列表
在状态模型中
class State extends \yii\db\ActiveRecord{
public static state($country){
if($country){
return ['state'];
}else{
return [];
}
}
}
城市模型也一样
如果 $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
);
这是国家/地区城市相关下拉功能,其中城市是多项选择
这是视图
<?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>";
}
}
你可以做得越来越简单。例如,我有带眼镜的剧院,所以我的 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']),
]...
当我选择剧院时,我只有这个剧院的眼镜下拉列表。不选剧场的时候,我有所有剧场所有可能的眼镜。