DepDrop Widget Kartik Yii2

4

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(), [
'options'=>['id'=>'proveedor-localidad_idlocalidad'],
'pluginOptions'=>[
    'depends'=>['proveedor-provincia_idprovincia'],
    'placeholder'=>'Selecciona una localidad...',
    'url'=>Url::to(['/localidad/localidades'])
]
]); ?> 
<?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);

    print_r($out);

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

In the locality model:

public static function getLocalidades($provid) 
{

    $data=  \app\models\Localidad::find()
   ->where(['provincia_idprovincia'=>$provid])
   ->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
source

1 answer

2

Be sure to delete the line that says:

print_r($out);

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'];

By

 $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:

{
    "output":[
        {"id":"1","name":"Mobile Phones"},
        {"id":"2","name":"Tablets"},
        {"id":"3","name":"Computers & Accessories"},
        {"id":"4","name":"Cameras"},
        {"id":"5","name":"Televisons"}
    ],
    "selected":"3"
}

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