DepDrop Widget Kartik Yii2


First consultation I do after removing the chestnuts a thousand times on this page, but I'm already somewhat desperate with a topic that should be or should be simple.

Use DepDrop of the Kartik libraries for Yii2, an excellent job. I need to use the dependent drep drop plugin, it seemed easy, but it has been impossible for me.

I have the tables and models provincia and localidad .

In the view I have:

<?php $form = ActiveForm::begin(); ?>

<?php  $provinciaList = ArrayHelper::map(Provincia::find()->orderBy('provincia')->all(), 'idprovincia', 'provincia'); ?>

<?= $form->field($model, 'provincia_idprovincia')->dropDownList
        ($provinciaList, ['id'=>'proveedor-provincia_idprovincia']);?>

$form->field($model, 'localidad_idlocalidad')->widget(DepDrop::classname(), [
    'placeholder'=>'Selecciona una localidad...',
]); ?> 
<?php ActiveForm::end(); ?>

In the controller of the location called by 'url'=>Url::to(['/localidad/localidades']) :

public function actionLocalidades() {
    $out = [];

    if (isset($_POST['depdrop_parents'])) {
    $parents = $_POST['depdrop_parents'];

    if ($parents != null) {

    $provid = $parents[0];

    $out = Localidad::Localidades($provid);


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

In the locality model:

public static function getLocalidades($provid) 

    $data=  \app\models\Localidad::find()
   ->select(['idlocalidad as id','localidad as name'])->asArray()->all();

   return $data;

In the first dropbox all the provinces appear, when you select a sale in the dependent field a Loading, but the list is not loaded.

Thank you if anyone can help me out.

asked by Jorge 09.03.2016 в 02:21

1 answer


Be sure to delete the line that says:


This generates an output that is not in the format expected by the plugin.

If the problem persists despite this it replaces the lines:

 if (isset($_POST['depdrop_parents'])) {
    $parents = $_POST['depdrop_parents'];


 $parents = "algun idprovincia valido"

And adjust the corresponding}.

Then enter your webpage / locality / locations to verify that the output is in a format similar to the following:

        {"id":"1","name":"Mobile Phones"},
        {"id":"3","name":"Computers & Accessories"},

When you get the proper output, replace the lines again and} to their original state.

 if (isset($_POST['depdrop_parents'])) {
    $parents = $_POST['depdrop_parents'];
answered by 30.11.2016 в 00:03