我使用Kartik editable扩展在Yii2中为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的正确方法是什么?
发布于 2018-06-20 08:21:33
这是一个Ajax可编辑的列。一次只有一个值会被发送到控制器。
因此post_title和post_description列的类必须在视图中是可编辑的。
每次编辑这些列时,这应该都会起作用。
也要改变这个
if ($model->load($post)) {
if (isset($model->post_title)){
// here you can format the value of the attribute
and display on the screen
$output = $model->post_title;
}
if (isset($model->post_description)){
$output = $model->post_description;
}
$model->save();
// Here you send a message that the value has been saved
$out = Json::encode(['output'=>$output, 'message'=>'Saved']);
}https://stackoverflow.com/questions/33806015
复制相似问题