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.