can I create a dependent dropdown in yii2?
I have two tables:
\'id\',\'name_country\"
\'id\',\'name_city\',\'country_id\'
and have
The above code is not working properly. There is an error in the line
$.post("'.Yii::$app->urlManager->createUrl('city/lists&id=').'"+$(this).val(),function( data )
console shows the error : Not Found (#404): Unable to resolve the request: subcategory/lists&id=54
is there any solution for this my controller looks like below
public function actionLists($id)
{
$posts = SubCategory::find()
->where(['category_id' => $id])
->orderBy('id DESC')
->all();
if($posts){
foreach($posts as $post){
echo "";
}
}
else{
echo "";
}
}
when i remove the id from the url and hard coded it in to controller it works properly.
I have find a solution for this please change your view as follows
= $form->field($model, 'category_id')->dropDownList($data,['prompt'=>'-Choose a Category-',
'onchange'=>'
$.get( "'.Url::toRoute('product/catlists').'", { id: $(this).val() } )
.done(function( data )
{
$( "select#product-sub_categoryid" ).html( data );
});
']); ?>
and controller like this
public function actionCatlists($id)
{
$mymodel = new Product ();
$size = $mymodel->modelGetCategory ( 'product_sub_category',$id );
if($size){
echo '';
foreach($size as $post){
echo "";
}
}
else{
echo '';
}
}
don't forget to include this on your view
use yii\helpers\Url;