Answer the question
In order to leave comments, you need to log in
How to make linked lists yii2?
Knowledgeable people tell me what I'm doing wrong? I just started learning yii2, I needed to make linked lists - choosing a region and cities, I do this (code example below), when choosing a region, the city field is not filled, it just becomes empty.
View:
<? $form = ActiveForm::begin(); ?>
<?php
echo $form->field($form_model, 'reg')->dropDownList(
ArrayHelper::map(Region::find()->all(), 'reg_id', 'reg_name'),
[
'onchange' => '
$.post(
"' . Url::toRoute('chusovoi') . '",
{id: $(this).val()},
function(data){
$("#SelectCit").html(data);
}
);
',
]
);
echo $form->field($form_model, 'cit')->dropDownList(ArrayHelper::map(City::find()->where(['cit_reg_id' => $id])->asArray()->all(), 'cit_id', 'cit_name'),
[
'prompt' => 'Выберите город',
'id' => 'SelectCit'
]
);
?>
<?php ActiveForm::end(); ?>
if ($id = Yii::$app->request->post('id')) {
$operationPosts = City::find()
->where(['cit_reg_id' => $id])
->count();
if ($operationPosts > 0) {
$operations = City::find()
->where(['cit_reg_id' => $id])
->all();
foreach ($operations as $operation){
echo "<option value='" . $operation->cit_id . "'>" . $operation->cit_name . "</option>";
}
} else
echo "<option>-</option>";
Answer the question
In order to leave comments, you need to log in
Instead:
It would be better if you loaded the list into JSON and using the API of some Select2 created the necessary list.
Yii2 dependent select what is wrong?
I asked a similar question yesterday, but I myself already found an error, everything is working there
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question