Yii2: How to Create Editable Row in Gridview

Share

Setup 1 – View

Put the following code inside gridview:

[
                'class'=>'kartik\grid\EditableColumn',
                'attribute' => 'nilai_tugas',
                'value' => function ($model)
                {
                    return $model->nilai_tugas;
                },
                'editableOptions'=> function ($model, $key, $index) {
                    return [
                        'header'=>'Name', 
                        'size'=>'md',
                        'formOptions'=>['action' => ['krs-detail/editNilai']],
                        'inputType'=>\kartik\editable\Editable::INPUT_TEXT,
                    ];
                }
 ],

Setup 2 – Controller

Put the following code into controller file:

use kartik\grid\EditableColumnAction;
use yii\helpers\ArrayHelper;
use backend\models\KrsDetail;

and then create function:

public function actions()
{
        return ArrayHelper::merge(parent::actions(), [
            'editNilai' => [                                       // identifier for your editable action
                'class' => EditableColumnAction::className(),      // action class name
                'modelClass' => KrsDetail::className(),            // the update model class
            ]
        ]);
}

Below is the result of editable Gridview:

Leave A Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.