【发布时间】:2015-11-19 14:00:32
【问题描述】:
我在 Yii2 中使用 Kartik Editable 扩展为 GridView 设置了可编辑列。我面临的问题是我找不到从一个可编辑列更新多个表格单元格的方法。 我做的事情: GridView 列
[
'class' => 'kartik\grid\EditableColumn',
'attribute'=>'post_title',
'editableOptions'=> function ($model, $key, $index) {
return [
'inputType' => \kartik\editable\Editable::INPUT_TEXT,
'size'=>'sm',
'afterInput'=>function ($form, $widget) use ($model, $index) {
return $form->field($model, 'post_description')->textInput(['placeholder'=>'Enter post title']);
}
];
}
],
通过单击编辑帖子标题列,它会显示标题和描述的编辑字段
PostsController 操作
public function actionIndex()
{
$searchModel = new PostsSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
if (Yii::$app->request->post('hasEditable')) {
$postId = Yii::$app->request->post('editableKey');
$model = Posts::findOne($postId);
$out = Json::encode(['output'=>'', 'message'=>'']);
$post = [];
$posted = current($_POST['Posts']);
$post['Posts'] = $posted;
if ($model->load($post)) {
$output = '';
$out = Json::encode(['output'=>$output, 'message'=>'']);
$model->save();
}
echo $out;
return;
}
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
所以,当我编辑帖子标题和描述时,只有帖子标题会保存到数据库中。 我认为是因为 current 只保存了一个值
$posted = current($_POST['Posts']);
保存两者的正确方法是什么 $model->post_title 和 $model->post_description ?
【问题讨论】: