dependent dropdown yii2. How to do?

前端 未结 4 1890
野的像风
野的像风 2020-12-05 21:58

can I create a dependent dropdown in yii2?

I have two tables:

\'id\',\'name_country\"
\'id\',\'name_city\',\'country_id\'

and have

4条回答
  •  渐次进展
    2020-12-05 22:37

    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

     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;
    

提交回复
热议问题