depdrop widget: function getSubCatList

I’m trying to use depdrop widget http://demos.krajee.com/widget-details/depdrop for a simple 2-level nested dependency.

I have a card model.

In card view form I have :




echo $form->field($model, 'album')->dropDownList(

    ArrayHelper::map(Album::find()->all(),'album_id','album_name'), 

    ['id'=>'album_id']);


echo $form->field($model, 'section')->widget(DepDrop::classname(), [

    'data'=>[9 => 'Serie B'],

    'options'=>['id'=>'section_id', 'placeholder'=>'Select ...'],

    'pluginOptions'=>[

        'depends'=>['album_id'],

        'url'=>Url::to(['subcat']),

         ]

]);



I think my problem is the output




public function getSubCatList($id) {

        $data=Album_section::find()

       ->where(['album_id'=>$id])

       ->select(['section_id','section_name'])->all();

            return $data;

        }



when I change album, firebug shows this JSON




output

	

	[Object { section_id=8, section_name="Serie A TIM"}, Object { section_id=9, section_name="Serie B"}]

0

	

	Object { section_id=8, section_name="Serie A TIM"}

section_id

	

	8

section_name

	

	"Serie A TIM"

1

	

	Object { section_id=9, section_name="Serie B"}

section_id

	

	9

section_name

	

	"Serie B"

selected

	

	""



And the section field has only:

0

blank row

blank row

1

blank row

blank row

Someone could please help me?

Thank you very much

Ok I found the solution, All the code is ok, actually the depdrop widget looks for the pair id and name like:

// the getSubCatList function will query the database based on the

    // cat_id and return an array like below:


    // [


    // ['id'=>'<sub-cat-id-1>', 'name'=>'<sub-cat-name1>'],


    // ['id'=>'<sub-cat_id_2>', 'name'=>'<sub-cat-name2>']


    // ]

Accordingly I have changed the code in the model


->select(['section_id AS id','section_name AS name'])->asArray()->all();

That’s all and it is working fine now. Hope someone will find this useful.

1 Like

thank you for posting this! helped me to find my error.