Answer the question
In order to leave comments, you need to log in
How to make form submit and autoupdate ListView?
Hello. I understand that the topic is rather hackneyed, but after a day of trials, nothing happened. The bottom line is this: there is a widget, it contains a form for adding a comment and a view sheet
<?
$form = ActiveForm::begin(['id' => 'form-feedback',
'enableAjaxValidation' => true,
'action' => '/feedback/index'
]);?>
?>
<?= $form->field($model, 'name')->textInput(['value' => !Yii::$app->user->isGuest ? Yii::$app->user->identity->username : '']) ?>
<?= $form->field($model, 'email')->textInput(['type' => 'email', 'value' => !Yii::$app->user->isGuest ? Yii::$app->user->identity->email : ''])?>
<?= $form->field($model, 'comments')->textarea(['rows' => 5]) ?>
<?= $form->field($model, 'id_obj')->textInput(['type' => 'hidden', 'value' => $id_obj])->label(false)?>
<?= $form->field($model, 'type')->textInput(['type' => 'hidden', 'value' => $type])->label(false)?>
<div class="form-group">
<?= Html::submitButton('<span>Отправить</span>', ['class' => 'btn btn-primary reg-btn', 'name' => 'signup-button']) ?>
</div>
<?php ActiveForm::end(); ?>
<?= ListView::widget([
'dataProvider' => $dataProvider,
'itemView' => '_list',
'layout' => "{summary}\n{items}\n{pager}",
'summary' => '',
'itemOptions' => [
'tag' => 'div',
'class' => 'feedback',
],
]);
?>
public function actionIndex(){
$model = new Feedback;
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($model);
}
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return Yii::$app->getResponse()->redirect(['/']);
}
}
Answer the question
In order to leave comments, you need to log in
<?php
Pjax::begin(['id'=>'pjax-container-form']);
$form = ActiveForm::begin(['id' => 'form-feedback',
'enableAjaxValidation' => true,
'action' => '/feedback/index',
'options' => ['data-pjax' => true]
]);?>
<?= $form->field($model, 'name')->textInput(['value' => !Yii::$app->user->isGuest ? Yii::$app->user->identity->username : '']) ?>
<?= $form->field($model, 'email')->textInput(['type' => 'email', 'value' => !Yii::$app->user->isGuest ? Yii::$app->user->identity->email : ''])?>
<?= $form->field($model, 'comments')->textarea(['rows' => 5]) ?>
<?= $form->field($model, 'id_obj')->textInput(['type' => 'hidden', 'value' => $id_obj])->label(false)?>
<?= $form->field($model, 'type')->textInput(['type' => 'hidden', 'value' => $type])->label(false)?>
<div class="form-group">
<?= Html::submitButton('<span>Отправить</span>', ['class' => 'btn btn-primary reg-btn', 'name' => 'signup-button']) ?>
</div>
<?php ActiveForm::end(); ?>
<?php Pjax::end(); ?>
<?Pjax::begin(['id'=>'pjax-container-comments']);?>
<?= ListView::widget([
'dataProvider' => $dataProvider,
'itemView' => '_list',
'layout' => "{summary}\n{items}\n{pager}",
'summary' => '',
'itemOptions' => [
'tag' => 'div',
'class' => 'feedback',
],
]);
?>
<? Pjax::end();?>
<?php
$this->registerJs('
$("#pjax-container-form").on("pjax:end", function() {
var url = "' . Url::toRoute(['/feedback/index']) . '";
$.pjax({url: url, container: "#pjax-container-comments"});
})');
?>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question