【发布时间】:2016-06-16 08:30:36
【问题描述】:
我有一个内联可编辑表(为此我使用了Tabledit),每一行都有一个 ID,ID 应该传递给控制器操作 (Yii2),以便我将编辑后的数据保存到数据库中。这是我的 js 文件中的 Tabledit 代码:
file.assetID = info.response; // the ID
for (var i = 0; i < file.length; i++) { // the table
if (file[i].type == "image/jpeg") {
var type = "photo";
} else if (file[i].type == "video/mp4") {
var type = "video";
}
messageHtml += '<tr id="' + file[i].assetID + '">';
messageHtml += '<td style="display:none;" id="' + file[i].assetID + '">' + file[i].assetID + '</td>';
messageHtml += '<td>' + file[i].name + '</td>';
messageHtml += '<td>' + type + '</td>';
messageHtml += '<td>' + file[i].size + " KB" + '</td>';
messageHtml += '<td><input type="text" class="form-control" placeholder="Tag"></td>';
messageHtml += '<td><input type="text" class="form-control" placeholder="Description"></td>';
messageHtml += '</tr>';
}
var urlID = "../save-inline-edit/" + file[0].assetID; // url plus the ID of the row
$('#uploader_table').Tabledit({
url: urlID,
columns: {
identifier: [0, 'id'],
editable: [[1, file.name]/*, [3, file.tag], [4, file.description]*/]
},
onSuccess: function(data, textStatus, jqXHR) {
console.log(data);
console.log(textStatus);
console.log(jqXHR);
},
onFail: function(jqXHR, textStatus, errorThrown) {
console.log(file.assetID);
console.log(jqXHR);
console.log(textStatus);
console.log(errorThrown);
}
});
我期待它会在保存内联编辑后指向指定的 url(urlID 其中save-inline-edit 是我的控制器中的一个操作函数--public function actionSaveInlineEdit($id){...}),但是当我检查元素时(保存后) ,它给了我这个错误:
然后我放置了一个console.log 来查看错误详细信息,我得到了这个:
“错误请求(#400):缺少必需的参数:id”
这是我的控制器操作:
public function actionSaveInlineEdit($id)
{
header('Content-Type: application/json');
$assetModel = $this->findModel($id);
$input = filter_input_array(INPUT_POST);
if ($input['action'] === 'edit') {
$assetModel->title = "";
$assetModel->description = "";
$assetModel->save(false);
} else if ($input['action'] === 'delete') {
$assetModel->status = "deleted";
$assetModel->save(false);
}
echo json_encode($input);
return \yii\helpers\Json::encode([
'message' => 'success',
]);
}
我真的不知道如何解决这个问题。如何将 id 传递给控制器?我希望你明白这一点。如果您有任何问题,请告诉我。如果您对实施有其他想法,也请告诉我。
【问题讨论】:
-
如果您将其作为
parameter传递,那么我猜您的url应该是var urlID = "../save-inline-edit?" + file[0].assetID;,即附加?。 -
在 yii2 中,特别是在我正在工作的这个项目中,url、美化和 ID 在动作名称后面的斜杠后面传递,在我的例子中是
../save-inline-edit/[ID] -
不确定,但您的操作名称似乎是
actionSaveInlineEdit,在urlID中是save-inline-edit? -
yii2的函数命名中就相当于
-
基本上错误表示您没有将
id参数提供给操作。不知道如何通过yii2传递,因为我对此一无所知..
标签: javascript php jquery yii frontend