Answer the question
In order to leave comments, you need to log in
How to make a modal window for editing GridView items?
The base is generated in CRUD.
At the moment there is:
public function actionIndex() {
$searchModel = new ContactsSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
/**
* Creates a new Contacts model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
$model = new Contacts();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'model' => $model,
]);
}
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\bootstrap\Modal;
/* @var $this yii\web\View */
/* @var $model backend\models\Contacts */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="contacts-form">
<?php Modal::begin([
'header' => 'Обновление контакта: '.$model->name,
]);
$form = ActiveForm::begin();
?>
<?= $form->field($model, 'name')->textInput(['maxlength' => true, 'id'=> 'nameUpdate']) ?>
<?= $form->field($model, 'email')->textInput(['maxlength' => true, 'id'=> 'emailUpdate']) ?>
<?= $form->field($model, 'subject')->textInput(['maxlength' => true, 'id'=> 'subjectUpdate']) ?>
<?= $form->field($model, 'body')->textInput(['maxlength' => true, 'id'=> 'bodyUpdate']) ?>
<div class="form-group">
<?= Html::submitButton('Обновить', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
<?php Modal::end(); ?>
</div>
public function actionUpdate() {
if (Yii::$app->request->isAjax) {
$id = Yii::$app->request->post('id');
$user = $this->findModel($id);
$userArray = [
'name' => $user->name,
'email' => $user->email,
'subject' => $user->subject,
'body' => $user->body,
];
echo json_encode($userArray);
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question