我有一個內聯可編輯表(我用Tabledit來表示這個),每一行都有一個ID,這些ID應該傳遞給控制器動作(Yii2)以便我保存編輯數據到數據庫。下面是我的js文件我TABLEDIT代碼:傳遞並從JavaScript獲取ID到PHP控制器
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);
}
});
我期待它會指向(urlID
其中save-inline-edit
是我controller-- public function actionSaveInlineEdit($id){...}
動作功能)指定的URL保存聯編輯後,但作爲我檢查元素(保存後),它給了我這個錯誤:
然後我把一個console.log
查看錯誤的詳細信息,我得到這個:
"Bad Request (#400): Missing required parameters: 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'應該是'URLID VAR = 「../save-inline-edit?」 +文件[0] .assetID;'附加'?'。 –
在yii2中,特別是在這個項目中,我正在工作,網址和身份驗證信息都是在我的案例''/ save-inline-edit/[ID]' – kaynewilder
之後的斜槓後面傳遞的。但你的動作名稱似乎是'actionSaveInlineEdit'和'urlID'中的'save-inline-edit'? –