【问题标题】:Yii2 Kartik-v Editable Column update multiple table cellsYii2 Kartik-v 可编辑列更新多个表格单元格
【发布时间】: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 ?

【问题讨论】:

    标签: php gridview yii2


    【解决方案1】:

    这是一个 Ajax 可编辑列。一次只会向控制器发送一个值。

    所以post_titlepost_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']);                
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-17
      相关资源
      最近更新 更多