使用EditableColumn在GridView中的一个RAW中更新两个单元格



我在YII2应用中具有可编辑的GridView,并使用Kartik EditableColumn。当只有一个可编辑的列时,一切都可以正常工作,但是当我尝试添加一列列时,它保存了 PRICE 列的编辑值,并将设置为零 expire_days 列的值。相反的方式相同(如果Update Expire_Days 它保存值,但将其设置为 Price value(。

视图:

'columns' => [
    [
        'class'=>'kartikgridEditableColumn',
        'attribute' => 'price',
        'editableOptions'=>[
            'header'=>' ',                         
            'inputType'=>kartikeditableEditable::INPUT_SPIN,
            'options'=>['pluginOptions'=>['min'=>0, 'max'=>5000000]]
        ],
        'value' => function ($model) {
            return $model->price;
         },
        'filter' => Category::getPrices(),
        'format' => 'raw'
    ],

    [
        'class'=>'kartikgridEditableColumn',
        'attribute' => 'expire_days',
        'editableOptions'=>[
            'header'=>' ',
            'inputType'=>kartikeditableEditable::INPUT_SPIN,
            'options'=>['pluginOptions'=>['min'=>0, 'max'=>100]]
         ],
        'value' => function ($model) {
            return $model->expire_days;
         },
        'filter' => Category::getExpDays(),
        'format' => 'raw'
    ],                                         
]

控制器:

if (Yii::$app->request->post('hasEditable')) {
    $model = new Category();
    $id = Yii::$app->request->post('editableKey');
    $model = Category::findOne($id);
    $out = Json::encode(['output'=>'', 'message'=>'']);
    $post = [];
    $posted = current($_POST['Category']);
    $post['Category'] = $posted;
    if ($model->load($post)) {
        $model->price = $post['Category']['price'];
        $model->expire_days = $post['Category']['expire_days'];
        $model->save();
        $output = '';
        $out = Json::encode(['output'=>$output,'message'=>'']);
    }
    echo $out;
    return;
}

您应该单独处理两个方案:

if ($model->load($post)) {
    if ($post['Category'] && $post['Category']['price']) {
        $model->price = $post['Category']['price'];
    }
    if ($post['Category'] && $post['Category']['expire_days']) {
        $model->expire_days = $post['Category']['expire_days'];
    }
    $model->save();
    $output = '';
    $out = Json::encode(['output'=>$output,'message'=>'']);
}

如果您收到价格 - 仅更新价格。如果您收到Expire_Days-更新Expire_Days。

最新更新