我一直在尝试从我的 HandsonTable 中删除行(它显示数据库中的行),但我找不到使用“删除行”选项删除它们的解决方案。
我尝试使用ajax并将表中的ID传递到另一个文件中以在数据库中删除,但没有删除任何内容。
function deleteRowsFromDatabase(rowToDelete) {
var idToDelete = rowToDelete[0];
$.ajax({
type: 'POST',
url: 'delete_com.php',
data: { ids: [idToDelete] },
success: function(response) {
console.log('Rows deleted successfully:', response);
if (response.length > 0 && response[0].ID) {
var deletedID = response[0].ID;
var rowIndex = hot.getData().findIndex(function(row) {
return row[0] === deletedID;
});
if (rowIndex !== -1) {
hot.alter('remove_row', rowIndex);
}
}
},
error: function(xhr, status, error) {
console.error('Error deleting rows:', error);
}
});
var rowIndex = hot.getSourceData().indexOf(rowToDelete);
if (rowIndex !== -1) {
hot.alter('remove_row', rowIndex);
}
}
hot.addHook('afterRemoveRow', function(index, amount) {
var removedRows = loadedData.slice(index, index + amount);
console.log('ammount', amount, 'index', index)
deleteRowsFromDatabase(removedRows);
});
编辑:
我发现,如果我删除下面有另一行的行,它会发送此数据,但会发送已删除行下面的行。
例如:我删除了 ID 为 5 的行。它会(暂时)从表中删除该行,并在控制台日志中显示它正在尝试删除 ID 为:6 的行。
解决了索引行问题:
hot.addHook('beforeRemoveRow', function (index, amount) {
var removedRows = loadedData.slice(index, index + amount);
console.log('amount', amount, 'index', index);
hot.addHookOnce('afterRemoveRow', function () {
deleteRowsFromDatabase(removedRows);
});
});
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['ids'])) {
$ids = $_POST['ids'];
$idList = implode("','", $ids);
$stmt = $conn->prepare("DELETE FROM com WHERE ID IN ('$idList') AND company = ?");
$stmt->bind_param("i", $compid);
$stmt->execute();
$stmt->close();
echo "Rows deleted successfully", $idList;
} else {
echo "No IDs sent";
}
}
这是delete_com.php 中的PHP 代码。它应该删除具有已选择的 ID 的行,但它只是传递了此代码并在控制台中显示“行已成功删除数组”,但它没有。
function deleteRowsFromDatabase(idToDelete) {
$.ajax({
type: 'POST',
url: 'delete_com.php',
data: { ids: idToDelete }, // Send only the ID
success: function (response) {
console.log('Row deleted successfully:', response);
},
error: function (xhr, status, error) {
console.error('Error deleting row:', error);
}
});
}
hot.addHook('beforeRemoveRow', function (index, amount) {
var removedRowIds = [];
for (var i = index; i < index + amount; i++) {
var row = hot.getDataAtRow(i);
removedRowIds.push(row[0]);
}
hot.addHookOnce('afterRemoveRow', function () {
for (var i = 0; i < removedRowIds.length; i++) {
deleteRowsFromDatabase(removedRowIds[i]);
}
});
});
我已将 hot.addHook('afterRemoveRow') 更改为 hot.addHook('beforeRemoveRow') 因为我要删除的行在发送到 delete_com.php 之前已被切片。我已经这样做了,所以它在数据发送到delete_com.php后对其进行切片。我也这样做了,所以它只收集行中的 ID,而不是整行,因为 ID 是唯一需要的东西。
include "sessions.php";
include "../../db/conn.php";
$compid = $_SESSION['company'];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['ids'])) {
$idToDelete = $_POST['ids'];
$stmt = $conn->prepare("DELETE FROM com WHERE ID = ? AND company = ?");
$stmt->bind_param("si", $idToDelete, $compid);
$stmt->execute();
$stmt->close();
echo "Row deleted successfully: $idToDelete";
} else {
echo "No ID sent";
}
}
$conn->close();
这是delete_com.php,我删除了sql 语句中的WHERE IN,因为不需要它。现在它将使用 $_POST['ids'] 中发送的数据(仅是行的 ID)。如果该ID在数据库中,它将被删除。