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