Yii2 - Kartik/EditableColumn 在 GridView 中不保存值

Yii2 - Kartik/EditableColumn in GridView does not save the value

我在使用 Yii2 中的 EditableColumn 时遇到问题,它没有保存更改的值, 在我的 GridView 索引中,我有以下内容:

[
   'class'=>'kartik\grid\EditableColumn',
   'attribute'=>'nombreDestino',
   'editableOptions' => [
   'inputType' => Editable::INPUT_DROPDOWN_LIST,
   'data'=> $claveCliente,
   'formOptions' => [
                  'action' => \yii\helpers\Url::to(['pru', 
                  ['id'=>$idOrigen,'idD'=>$idDestino]])
                    ]
 ],

在我的控制器中我有以下内容:

public function actionPru()
    {
        $val = implode(",",$_GET[1]['id']);
        $val2 = implode(",",$_GET[1]['idD']);

        if(Yii::$app->request->post('hasEditable'))
        {
            $nombreDestino = Yii::$app->request->post('editableKey');
            $Destino = RelClientes::findOne($nombreDestino);

            $out = Json::encode(['output'=>'','message'=>'']);
            $post = [];
            $posted = current($_POST['RelClientes']);

            if($Destino->load($posted))
            {
                $Destino -> save(false);
            }
            echo $out;
            return;
        }
    }

JSON returns 对我来说是空的,当进行更改并单击保存按钮时,如果我在 GridView 中进行更改但在重新加载页面时更改不会保存。

RelClientes 是我的模型。

请帮忙。

确保您使用的是 Kartik\grid\Gridview,而不是 yii\grid\GridView。您应该查看 EditableColumnAction 的 DOCS 以配置更新操作,您不需要传递任何 id.

Processing Editable Data

In addition to the editable input value that will be returned via form POST action, the Editable Column automatically stores the following hidden inputs, for retrieval via your controller action: - editableIndex the grid row index to which the editable data belongs.

  • editableKey the grid primary key to which the editable data belongs. If the grid's data has a primary key which is numeric or string, then it would be returned as is. However, if the grid data has a composite primary key (array) or an object as a key (as used in mongo db), then this will return a PHP serialized string, that can be parsed using the PHP unserialize method.

所以在你的控制器中用下面的函数替换你的动作

public function actions() {

        return yii\helpers\ArrayHelper::merge ( parent::actions () , [
                    'pru' => [
                        'class' => kartik\grid\EditableColumnAction::class ,
                        'modelClass' => RelClientes::class ,
                        'outputValue' => function ($model , $attribute , $key , $index) {
                            return $model->$attribute;
                        } ,
                        'outputMessage' => function($model , $attribute , $key , $index) {
                            return '';
                        } ,
                    ]

                    ]);
    }

并将您的 EditableColumn 定义更新为以下内容

[
    'class' => kartik\grid\EditableColumn::class ,
    'attribute' => 'name' ,
    'editableOptions' => [
        'inputType' => Editable::INPUT_DROPDOWN_LIST ,
        'data'=> $claveCliente,
        'formOptions' => [
           'action' => \yii\helpers\Url::to([ 'pru' ])
        ]
    ] ,
] ,

希望对您有所帮助。