How to save data in model using Yii2 grid with Edi

2020-07-26 11:38发布

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.

1条回答
够拽才男人
2楼-- · 2020-07-26 12:05

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.

查看更多
登录 后发表回答