D
D
Danil Solovyov2016-09-29 21:01:30
Yii
Danil Solovyov, 2016-09-29 21:01:30

How to make a modal window for editing GridView items?

The base is generated in CRUD.
At the moment there is:

  • The _form.php file which describes the modal window
  • Controller slightly modified to open the new element creation modal
  • GridView wrapped in Pjax

Changes made to the controller:
  • ActionCreate moved to ActionIndex

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,
        ]);
    }

I ask you not to kick with your feet, I just started learning Yii2.
UPD:
The _update.php file has been created, which describes the modal window for updating the element.
<?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>

Rewritten controller\ActionUpdate
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);
        }
    }

To this all, an Ajax request is naturally written, as well as updating the input values ​​​​to those that were requested from the controller.
BUT! The whole problem is that when rendering contacts/index, the $model parameter is passed, which was originally set to add a new element, so when the modal is called, the element is not updated, but a new one is created.
Question: how to override $model to update a given element, rather than create a new one.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mykola, 2016-09-29
@iSensetivity

1. Place an ajax request on the button.
2. create a method for returning the html code with the data loaded into it from the model.
3. create a save method.
4. Profit!!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question