Answer the question
In order to leave comments, you need to log in
How to load form fields with ajax depending on category selection in Yii2?
When creating a post
I have installed the Select2 widget from Kartik demos.krajee.com/widget-details/select2
And I use it as Ajax Loading - you enter 3 letters and it finds the right category. It is necessary to make it so that after the category is found and selected, an additional field is displayed with a radio list of site names with links.
<?= $form->field($model, 'site')->radioList(ArrayHelper::map(Site::getSite($category_id), 'link', 'title'),
[
'item' => function($index, $label, $name, $checked, $value) use ($model) {
$check = $checked == 1 ? "checked='checked'" : null;
return '<label class="modal-radio" style="display:block;">
<input type="radio" name="' . $name . '" value="' . $value . '" ' . $check . ' tabindex="3">
<i></i>
<span>' . ucwords($label) . '</span>
</label>';
}
]
); ?>
jQuery(function ($) {
$(document).on("select2:select", "#post-category_id", function(e) {
if (($("#post-category_id").val() != null)) {
$.ajax({
url: "/post/post-specifics",
data: {
category_id: $("#post-category_id").val(),
},
method: "POST",
beforeSend: function(xhr) {
$(".wrapper_post_specifics").html("<h2 class=\"text-center\">Загрузка...</h2>");
}
}).done(function(data) {
$(".wrapper_post_specifics").html(data);
}).fail(function(data) {
alert("Во время загрузки Дополнительных полей потока произошла ошибка!");
});
}
})};
public function actionPostSpecifics(){
$model = new Post();
$model->load(Yii::$app->request->post());
$ category_id = $model-> category_id;
return $this->render('form_specifics', [
'model' => $model ,
'category_id' => $category_id,
]);
}
Answer the question
In order to leave comments, you need to log in
Good evening.
public function actionPostSpecifics(){
if(Yii::$app->request->isAjax){
$model = new Post();
$model->load(Yii::$app->request->post());
$category_id = $model-> category_id;
$form = new \yii\widgets\ActiveForm;
return $this->renderAjax('form_specifics', [
'model' => $model ,
'category_id' => $category_id,
'form' => $form
]);
}
}
echo $form->field($model, 'attribute_name')->textInput();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question