I have an inline editable table (I used Tabledit for this) and each row has an ID and the IDs should be passed to the controller action (Yii2) in order for me to save edited data to the database. Here's my Tabledit code in my js file:
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);
}
});
I was expecting that it would point to the url specified (urlID
where save-inline-edit
is an action function in my controller--public function actionSaveInlineEdit($id){...}
) after saving the inline edit, but as I inspect element (after saving), it gives me this error:
Then I placed a console.log
to view the error details and I get this:
"Bad Request (#400): Missing required parameters: id"
Here's my controller action:
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',
]);
}
I really don't know how to figure this out. How do I pass the id to the controller? I hope you understand this. Please let me know if you have questions. If you have other idea for the implementation, let me know as well.