Help with DepDrop

I want to use DepDrop, but I have a problem that I can’t solve, after select the parent, I see the child as disable and I don’t have information.

To verify the function I use this code in views\globe\_form.php


    $cat_id='2';

    $var=Globe::getSubCatList($cat_id);;

    print_r($var);

and it gives me this result:

Array ( [0] => Array ( [id] => 1 [name] => Buenos Aires ) [1] => Array ( [id] => 2 [name] => Cordoba ) [2] => Array ( [id] => 3 [name] => Santa Fe ) )

My result with this code is:

(I can’t write url, please look for in the reply)

An the database relationship is:

(I can’t write url, please look for in the reply)

Below you’ll find the code that I’m using.

views\globe\_form.php


	$country=ArrayHelper::map (Country::find()->asArray()->all(),'id','country_name');


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

        [

            'prompt'=>'Select ...',

            'id'=>'cat-id'

        ]);


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

        'options'=>['id'=>'subcat-id'],

        'pluginOptions'=>[

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

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

            'url'=>Url::to(['province/index'])

        ]

    ]);

controllers\GlobeController.php


	public function actionSubcat() {

        $out = [];

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

            $parents = $_POST['depdrop_parents'];

            if ($parents != null) {

                $cat_id = $parents[0];

                $out = Globe::getSubCatList($cat_id);

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

                return;

            }

        }

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

    }

models\Globe.Php


	public function getSubCatList($cat_id)

    {

        $data=Province::find()

            ->where(['country_id'=>$cat_id])

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


        return $data;

    }

My result with this code is:

(I can’t write url, please look for in the reply)

The database relationship is:

(I can’t write url, please look for in the reply)

I found the solution, I’ve changed this:

views\globe\_form.php


      'url'=>Url::to(['province/index'])

Correct:


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

and this:

models\Globe.Php


	public function getSubCatList($cat_id)

    {

        $data=Province::find()

            ->where(['country_id'=>$cat_id])

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


        return $data;

    }

for:

models\Province.Php


	public function getSubCatList($cat_id)

    {

        $data=Province::find()

            ->where(['country_id'=>$cat_id])

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


        return $data;

    }

While using depdrop or other widget that use ajax is very useful to use firebug or something similar that allow you to verify the ajax action.

Fore the people that does not know it (if there are still some :P) is an extension for firefox and chrome.

It has a lot of tool useful for debug.

On the ajax side there is a NET tab which has a XHR panel.

In XHR you can see all the details of the ajax call, url post and get, value, response…

This panels save me a lot of time while debugging ajax call.