我的 Yii2 应用程序中有一个可编辑的 GridView,并使用了 kartik EditableColumn。当只有一个可编辑列时,一切正常,但是当我尝试再添加一个可编辑列时,它会保存Price列的编辑值并将expire_days列的值设置为零。相反的方式相同(如果更新expire_days它保存值但将价格值设置为零)。风景:'columns' => [ [ 'class'=>'kartik\grid\EditableColumn', 'attribute' => 'price', 'editableOptions'=>[ 'header'=>' ', 'inputType'=>\kartik\editable\Editable::INPUT_SPIN, 'options'=>['pluginOptions'=>['min'=>0, 'max'=>5000000]] ], 'value' => function ($model) { return $model->price; }, 'filter' => Category::getPrices(), 'format' => 'raw' ], [ 'class'=>'kartik\grid\EditableColumn', 'attribute' => 'expire_days', 'editableOptions'=>[ 'header'=>' ', 'inputType'=>\kartik\editable\Editable::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;}
1 回答
一只甜甜圈
TA贡献1836条经验 获得超5个赞
您应该分别处理两种情况:
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。
- 1 回答
- 0 关注
- 216 浏览
添加回答
举报
0/150
提交
取消