DepDrop Widget help please.

Hi All,

I am using DepDrop Widget and all is working fine except the second dropdownlist is not showing the text.

It has 2 empty rows which is somewhat correct, because the database has 2 records which is should display. When selecting another parent, the sub list changes to 1 empty row which is also correct, but it shows empty/clean rows, no text is being displayed.

Any ideas?

Never mind, for others, your select sql must have columns id, name.

If your database has columns id, first_name, you must select as:

select id, first_name as name from…

The DepDrop extension looks for columns id and name, nothing else.

That’s correct. Its mentioned in the docs and examples. The JSON format to return for a normal dependent dropdown is:




{

    {id: 'id1', name: 'name1'},

    {id: 'id2', name: 'name2'},

}



If you want, you can also use depdrop with optgroups by returning JSON this way:




{

    'group 1': {

        {id: 'id1a', name: 'name1a'},

        {id: 'id1b', name: 'name1b'},

    },

    'group 2': {

        {id: 'id2a', name: 'name2a'},

        {id: 'id2b', name: 'name2b'},

    },

}



Refer DepDrop widget settings or dependent-dropdown jquery plugin docs for options to set.


Thank you,

Please note the


'options'=>['id'=>'productsid[]',

part.




<?php

	echo $form->field(new Product(), 'product')->widget(DepDrop::classname(), [

			'options'=>['id'=>'productsid[]', 'style'=>'width:600px'],

			'pluginOptions'=>[

			'depends'=>['company-id'],

			'placeholder'=>'Select...',

			'url'=>Url::to(['/invoice/productspercompany'])

			]

	]);

?>



When I do that, the dropdown does not work. How would I manage multiple dropdown’s with the same name? I want the user to be able to select more than 1 product and handle it as an array of products on the backend.

Thanks

Your id must still be unique and you must use attribute name in array format (read yii docs/api reference on this).




for ($i=0; $i<10; $i++) {

    // use prod id uniquely in your code

    $prodId = "products-id-{$i}";

    

    // note the attribute name

    echo $form->field($model, "[$i]product")->widget(DepDrop::classname(), [

        'options'=>['id'=>$prodId, 'style'=>'width:600px'],

        'pluginOptions'=>[

            'depends'=>['company-id'],

            'placeholder'=>'Select...',

            'url'=>Url::to(['/invoice/productspercompany'])

        ]

    ]);

}