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;
}
}