editable-datacolumn-for-gridview Editable DataColumn for GridView

  1. Requirements
  2. Installation
  3. Usage

The JZDataColumn is the extantion of DataColumn class used in GridView. Allowing user to edit data of string type directly in column.

Requirements

Yii 2.0 or above.

Installation

  1. Create folder "jz" in "vendor" in your project.
  2. Place JZDataColumn.php in "jz" folder.
  3. Place jzdatacolumn.js in you JavaScript folder.
  4. Place jzdatacolumn.css in your CSS folder.
  5. Make necessary changes to AppAsset.php.

Usage

In GridView:

Pjax::begin(['id' => 'pjax_id_1', 'options'=> ['class'=>'pjax', 'loader'=>'loader_id_1', 'neverTimeout'=>true]]);
    echo GridView::widget([
    	'dataProvider' => $dp,
        'filterModel' => $model,
        'options'=>['class'=>'grid-view','id'=>'grid_id_1'],
        'summaryOptions'=>['style'=>'text-align:right;'],
        'columns' => [
            [
        	'class' => 'vendor\jz\JZDataColumn',
                'attribute' => 'tbl_attribute',
                'editable' => true,
                'editUrl' => Url::to(['controller/jzdatacolumn']),
               	'emptyLabel' => 'Click me to add',
                'loaderPath' => Yii::getAlias('@cssImg').'/img_loader.gif',
                'headerOptions' => ['style'=>'text-align:center;'],
            ],
        ],
    ]); 
Pjax::end();

In controller:

public function actionJzDataColumn(){
    if(isset($_GET['data'])){
    	$json=json_decode($_GET['data']);
        if(isset($json->id) && isset($json->attribute) && isset($json->value)){
            $model=YourClass::find()->where(['id'=>$json->id])->one();
            if(isset($model)){
                $attribute=$json->attribute;
                $model->$attribute=trim($json->value);//variable variable used!!!
                if($model->save()){return json_encode(['msg'=>1,'val'=>$model->$attribute]);
                } else {return json_encode(['msg'=>0,'val'=>$model->firstErrors[$attribute]]);}
            } else {return json_encode(['msg'=>0,'val'=>'No model found.']);}
        } else {return json_encode(['msg'=>0,'val'=>'No correct dataset provided.']);}
    } else {return json_encode(['msg'=>0,'val'=>'No data provided.']);}
}
1 0
1 follower
0 downloads
Yii Version: 2.0.*
License: BSD-3-Clause
Category: User Interface
Developed by: Jay_69
Created on: Oct 4, 2018
Last updated: 5 years ago
Github Repository

Related Extensions