i am trying to create a dependent dropdown in yii2 basic but it is not working as expected. below is the code to create the dropdown
<?= $form->field($model,'grp_name')->dropDownList(
ArrayHelper::map( Maingroup::find()->all(), 'id', 'name'),
[
'prompt'=>'Select your group',
'onchange'=>' $.post( "index.php?r=memberdetail/lists&id='.'"+$(this).val(), function( data ) {
$( "select#memberdetail-sub_grp" ).html( data );
});'
]); ?>
<?= $form->field($model,'sub_grp')->dropDownList(
ArrayHelper::map(NewGroup::find()->all(), 'id', 'group_num'),
[
'prompt'=>'Select your sub-group',
]); ?>
my lists action in memberdetail controller is
public function actionLists($id)
{
$countsubgroup = NewGroup::find()
->where(['group_name' => $id])
->count();
$subgroup = NewGroup::find()
->where(['group_name' => $id])
->all();
if ($countsubgroup > 0) {
foreach ($subgroup as $name) {
echo "<option value='" . $name->id . "'>" . $name->group_num . "</option>";
}
} else {
echo "<option> - </option>";
}
}
what actually is happening is this problem i think due to which it is not going to the memberdetail controller and not calling to public function actionLists($id)
Simple way to Create dependent Dropdown
first create
DependentController.php
in yourcontroller
folder likeYour _form.php