首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Yii2 Kartik-v可编辑列更新多个表单元格

Yii2 Kartik-v可编辑列更新多个表单元格
EN

Stack Overflow用户
提问于 2015-11-19 22:00:32
回答 1查看 1.8K关注 0票数 1

我使用Kartik editable扩展在Yii2中为GridView设置了可编辑列。我面临的问题是,我无法找到一种方法来从一个可编辑的列更新多个表格单元格。我做的事情:GridView专栏

代码语言:javascript
复制
[
                    '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操作

代码语言:javascript
复制
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只保存了一个值

代码语言:javascript
复制
$posted = current($_POST['Posts']);

保存$model->post_title$model->post_description的正确方法是什么?

EN

回答 1

Stack Overflow用户

发布于 2018-06-20 08:21:33

这是一个Ajax可编辑的列。一次只有一个值会被发送到控制器。

因此post_titlepost_description列的类必须在视图中是可编辑的。

每次编辑这些列时,这应该都会起作用。

也要改变这个

代码语言:javascript
复制
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']);                
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33806015

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档