How to save data in model using Yii2 grid with Editable column

Rabib

Can anyone help on editable column in gridview.I am using Yii2 and stuck with it. I can't save data in my model.I can post from gridview column.

In my grid view:

$gridColumns= [
  'patient_no',
  'category_name',
  'sdv_text',
  [
    'class' => 'kartik\grid\EditableColumn',
    'attribute'=>'sdv_status',
    'pageSummary' => true,
    'editableOptions'=> [
      'header' => 'profile',
      'format' => Editable::FORMAT_BUTTON,
      'inputType' => Editable::INPUT_DROPDOWN_LIST,
      'data'=> $StatusList,
    ]
  ],
  //  'date_sdv_performed',
  [
    'class' => 'kartik\grid\EditableColumn',
    'attribute'=>'date_sdv_performed',
    'editableOptions' => [
      'header' => 'Date Sdv Performed',
      'inputType'=>\kartik\editable\Editable::INPUT_WIDGET,
      'format'=>\kartik\datecontrol\DateControl::FORMAT_DATE,
      'widgetClass'=> 'kartik\datecontrol\DateControl',
    ],
  ],
  [
    'class' => 'kartik\grid\EditableColumn',
    'attribute'=>'comments',
    'hAlign' => 'top',
    'vAlign' => 'middle',
    'width'=>'100px',
    'headerOptions' => ['class' => 'kv-sticky-column'],
    'contentOptions' => ['class' => 'kv-sticky-column'],
    'pageSummary' => true,
  ],
];
GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'layout'=>"{items}\n{pager}",
        'pjax'=>true,
        'toolbar' => [
          '{export}',
          '{toggleData}'
        ],
        'responsive'=>true,
        'hover'=>true,
        'columns' => $gridColumns
      ]);

In my controller action:

public function actionMonitoring($site_name)
  {
    $this->layout = 'sdv-carolina-main';
    $Countries      = new Countries;
    $model          = new Flagging;
    $searchModel    = new FlaggingSearch();
    $dataProvider   = $searchModel->monitoringsearch($site_name);
    $allocatedsites = new AllocatedSites;
    if (Yii::$app->request->post('hasEditable')) 
    {
      $model  = $this->findModel($model['flagging_id']);
      $out    = Json::encode(['output'=>'', 'message'=>'']);
      $post = [];
      $posted = current($_POST['Flagging']);
      $post['Flagging'] = $posted;
      if ($model->load($post)) {
        $model->save();
        $output = '';
        if (isset($posted['sdv_status'])) 
        {
          $output =  $model->sdv_status;
        }
         $out = Json::encode(['output'=>$output, 'message'=>'']); 
      }
      echo $out;
      return;
    }
    return $this->render('monitoring',
      [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
        'Countries' => $Countries,
        'model'=>$model,
        'allocatedsites' => $allocatedsites,
      ]);
  }

The problem is I can't update my model because of I can't get the id. I just need the id to update specific row.How can I get the id while using editable column?

Thanks in advance.

Rabib

Actually the solution is easy. I just need the id of that specific row to update that.And in my ajax post I got something like this:

Flagging[0][status] NO
_csrf   TlhyUm5kajAoNxgVNy0/ZCoyHApZUlNUFh0rB1gRPGoAFSIdGSAifQ==
editableIndex   0
editableKey 13
hasEditable 1

and found the editableKey is the id of that specific row! Now in my controller I write down this code given below:

$_id=$_POST['editableKey'];
$model = $this->findModel($_id);

Here $_id is the posted editableKey value which is the id of the specific row. and with the id I use it to get the specific model and just update data based on that id.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I save an encrypted data using Yii::$app->security->encryptByKey() in Yii2

How to export model data and relation model to json with Yii2?

How to save data using Model in Magento2

model->save() Not Working In Yii2

How to display data of another table in grid view in yii2?

How to get count from many relations in a Grid Yii2 Search Model

How to use Yii2 kartik gridview editable column with mulitple model relations

Yii2 Grid view syntax to access data from related model

Yii2 data-* attributes not rendered using GridView Column

How to get all column data from two tables by using leftJoin in Yii2

Yii2 cannot save model data in controller

Dynamic Kendo UI Grid editable datetime column not using value

How to conditionally set column to editable in kendo grid?

how to make a grid column editable when using custom store

Yii2 save model to database

Yii2 grid updating with editable field

Yii2 : How to change URL in Action Column Grid View?

Yii doesn't save editable grid after submit button

Save changes in editable data grid with enter key - xpages

How to get data using yii2 active records and relations?

How to save a model table programmatically in yii2

Yii2:how to use model's data in layouts

yii2 calculate selected row in grid-view (checkbox-column) using jquery and submit the data

Yii2 how to save received JSON data into database

How to make specific column of the Kendo Grid editable?

how to access Yii2 grid view data

How to get the data Using Yii2 Query Builder

Editable Angular data grid

How to get data from model A in view B Yii2