Editable grid

View:

Add:

use kartik\grid\GridView;

in GridView::widget add this

[
 'class'=>'kartik\grid\EditableColumn',
 'attribute'=> 'your_attribute',
],

Controller
Add:

use yii\helpers\Json;

In Index action add this:

    // validate if there is a editable input saved via AJAX
    if (Yii::$app->request->post('hasEditable')) {
      // instantiate your model for saving
      $id = Yii::$app->request->post('editableKey');
      $model = DocumentSeries::findOne($id);

      // store a default json response as desired by editable
      $out = Json::encode(['output' => '', 'message' => '']);

      // fetch the first entry in posted data (there should only be one entry
      // anyway in this array for an editable submission)
      // - $posted is the posted data for Book without any indexes
      // - $post is the converted array for single model validation

      $posted = current($_POST['DocumentSeries']);
      $post = ['DocumentSeries' => $posted];

      // load model like any single model validation
      if ($model->load($post)) {
        // can save model or do something before saving model
        $model->save();
      }

      // return ajax json encoded response and exit
      echo $out;
      return;

About

Das Yii Man

Categories: HINTS (tehnice) | Tags: ,

Leave a Reply

Your email address will not be published. Required fields are marked *

[TOP]