Kartik depdrop problem with option labels

So, i managed to understand and use this extension, but when i select the value from the dropdown, the other one charge the data successfully, but it dont show the name of the value im selecting, i mean, the options have the correct value attribute, but with no text to show, so it appears as a white list…

I have been trying to search and fix it but i couldnt do it, maybe you can help a bit

Form fields:


<?= $form->field($model, 'magazine_id')->dropDownList($magazines, ['prompt' => 'Seleccione magazine...', 'id' => 'nombre'])->label('Magazine') ?>


    <?= $form->field($model, 'nom_Cat')->widget(DepDrop::classname(), [

        'pluginOptions' => [

            'depends' => ['nombre'],

            'placeholder' => 'Seleccione categoria...',

            'url' => Url::to(['/mhas-categoria/nomcat'])

        ]

    ])->label('Categoria') ?>

Controller action that is called to fill the depdropdown


public function actionNomcat(){

        $out = [];

        if (isset($_POST['depdrop_parents'])) {

            $parents = $_POST['depdrop_parents'];

            if ($parents != null) {

                $mag_id = $parents[0];

                $out = self::getSubCatList($mag_id);

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

                // mag_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>']

                // ]

                if ($out) {

                    echo Json::encode(['output'=>$out, 'selected'=>'']);

                    return;

                }else{

                    echo Json::encode(['output'=>'', 'selected'=>'']);

                    return;

                }

                

            }

        }

        echo Json::encode(['output'=>'', 'selected'=>'']);

    }

Function that get the results from db


protected function getSubCatList($mag_id){


        $categorias = CategoriaMagazine::find()->all();


        $i = 0;

        $arr = array();

        foreach ($categorias as $key => $value) {

            $aux = MhasCategoria::find()->where(['nom_Cat' => $value->nombre, 'magazine_id' => $mag_id])->one();


            if (!$aux) {

                $arr[$i] = ['id' => $value->nombre, 'nombre' => $value->nombre]; //yes, this is intended, since the related column in my table is not the id, but the name of the category, i thought the second index in this array would be shown in the option label

            }

            $i++;

        }


        if ($arr) {

            Yii::$app->response->format = Response::FORMAT_JSON;

            return $arr;

        }

    }

Looking at you code you send back a $out in a format that is not expected by depdrop

https://github.com/k...endent-dropdown

according to the documentation depdrop expect the following json

{output: <dependent-list>, selected: <default-selected-value>}

where <dependent-list> is as well a josn with this format

{id: <option-value>, name: <option-name>}

looking at you code you named the label ‘nombre’ instead of ‘name’ which is what depdrop expect.

If I remember correctly you can rename the id and name filed in depdrop configuration, try to look at the documentation or just rename the field in your model

ok, thank you so much, it worked, changed "nombre" to "name" and now it displays properly the options labels… This is a great extension but difficult to make it work if you misunderstand something (like happened to me)