Yii2:要搜索的依赖下拉列表
Yii2 : Dependent Dropdown List to search
我想创建一个过滤器供用户搜索。我创建了这个依赖下拉列表,但它不起作用。我有 'city' 和 'state' 表。它不工作。
控制台错误 POST http://localhost/smarthouse/web/index.php?r=post/lists?id=1 404 (Not Found)
谁能告诉我哪里错了?
谢谢
我的表格
<?php
$dataCity=ArrayHelper::map(\app\models\Cities::find()->
asArray()->all(),'id', 'name');
$form = ActiveForm::begin();
echo $form->field($searchModel, 'id')->dropDownList($dataCity,
[''=>'-Choose a Name-',
'class'=>'adjust',
'onchange'=>'
$.post("index.php?r=post/lists?id='.
'"+$(this).val(),function( data )
{
$( "select#post" ).html( data );
});
']);
$dataState=ArrayHelper::map(\app\models\States::find()->
asArray()->all(), 'id', 'name');
echo $form->field($searchModel, 'id')
->dropDownList(
$dataState,
['id'=>'name',
'class'=>'adjust'
]
);
ActiveForm::end();
?>
我在 post 控制器中的列表操作
public function actionLists($id)
{
$countPosts = States::find()
->where(['city_id' => $id])
->count();
$posts = States::find()
->where(['city_id' => $id])
->orderBy('id DESC')
->all();
if($countPosts>0){
foreach($posts as $post){
echo "<option value='".$post->id."'>".$post->name."</option>";
}
}
else{
echo "<option>-</option>";
}
在你的 onchange $.post 路径中,第二个 GET 参数应该以 &
开头,而不是以 ?
这样的方式`index.php?r=post/lists&id
'onchange'=>'
$.post("index.php?r=post/lists&id='. // use & not ?
'"+$(this).val(),function( data )
{
$( "select#post" ).html( data );
});
'
我想创建一个过滤器供用户搜索。我创建了这个依赖下拉列表,但它不起作用。我有 'city' 和 'state' 表。它不工作。
控制台错误 POST http://localhost/smarthouse/web/index.php?r=post/lists?id=1 404 (Not Found)
谁能告诉我哪里错了? 谢谢
我的表格
<?php
$dataCity=ArrayHelper::map(\app\models\Cities::find()->
asArray()->all(),'id', 'name');
$form = ActiveForm::begin();
echo $form->field($searchModel, 'id')->dropDownList($dataCity,
[''=>'-Choose a Name-',
'class'=>'adjust',
'onchange'=>'
$.post("index.php?r=post/lists?id='.
'"+$(this).val(),function( data )
{
$( "select#post" ).html( data );
});
']);
$dataState=ArrayHelper::map(\app\models\States::find()->
asArray()->all(), 'id', 'name');
echo $form->field($searchModel, 'id')
->dropDownList(
$dataState,
['id'=>'name',
'class'=>'adjust'
]
);
ActiveForm::end();
?>
我在 post 控制器中的列表操作
public function actionLists($id)
{
$countPosts = States::find()
->where(['city_id' => $id])
->count();
$posts = States::find()
->where(['city_id' => $id])
->orderBy('id DESC')
->all();
if($countPosts>0){
foreach($posts as $post){
echo "<option value='".$post->id."'>".$post->name."</option>";
}
}
else{
echo "<option>-</option>";
}
在你的 onchange $.post 路径中,第二个 GET 参数应该以 &
开头,而不是以 ?
这样的方式`index.php?r=post/lists&id
'onchange'=>'
$.post("index.php?r=post/lists&id='. // use & not ?
'"+$(this).val(),function( data )
{
$( "select#post" ).html( data );
});
'