如何使用带有可编辑列的 Yii2 网格在模型中保存数据 [英] How to save data in model using Yii2 grid with Editable column

查看:20
本文介绍了如何使用带有可编辑列的 Yii2 网格在模型中保存数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮助处理 gridview 中的可编辑列.我正在使用 Yii2 并坚持使用它.我无法在我的模型中保存数据.我可以从 gridview 列发布.

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.

在我的网格视图中:

$gridColumns= [
  'patient_no',
  'category_name',
  'sdv_text',
  [
    'class' => 'kartikgridEditableColumn',
    'attribute'=>'sdv_status',
    'pageSummary' => true,
    'editableOptions'=> [
      'header' => 'profile',
      'format' => Editable::FORMAT_BUTTON,
      'inputType' => Editable::INPUT_DROPDOWN_LIST,
      'data'=> $StatusList,
    ]
  ],
  //  'date_sdv_performed',
  [
    'class' => 'kartikgridEditableColumn',
    'attribute'=>'date_sdv_performed',
    'editableOptions' => [
      'header' => 'Date Sdv Performed',
      'inputType'=>kartikeditableEditable::INPUT_WIDGET,
      'format'=>kartikdatecontrolDateControl::FORMAT_DATE,
      'widgetClass'=> 'kartikdatecontrolDateControl',
    ],
  ],
  [
    'class' => 'kartikgridEditableColumn',
    '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}
{pager}",
        'pjax'=>true,
        'toolbar' => [
          '{export}',
          '{toggleData}'
        ],
        'responsive'=>true,
        'hover'=>true,
        'columns' => $gridColumns
      ]);

在我的控制器操作中:

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,
      ]);
  }

问题是我无法更新我的模型,因为我无法获得 ID.我只需要更新特定行的 id.如何在使用可编辑列时获取 id?

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?

提前致谢.

推荐答案

其实解决方案很简单.我只需要该特定行的 id 来更新它.在我的 ajax 帖子中,我得到了这样的信息:

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

发现 editableKey 是该特定行的 id!现在在我的控制器中,我写下了下面给出的代码:

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);

这里的 $_id 是发布的 editableKey 值,它是特定行的 id.并使用 id 获取特定模型并根据该 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.

这篇关于如何使用带有可编辑列的 Yii2 网格在模型中保存数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆