the code below is my front code remember it i will ask multiple questions var dataTable;…
- User
the code below is my front code remember it i will ask multiple questions var dataTable;
$(document).ready(function () { loadList(); });
function loadList() { var url_string = window.location.href; var url = new URL(url_string); var SampleAnalyzeId = url.searchParams.get("id"); dataTable = ('#DT_load').DataTable({ "ajax": { "url": "/api/SampleAnalyzeFile/" + SampleAnalyzeId, "type": "GET", "datatype": "json" }, "columns": [ { "data": "File", "width": "25%" }, { "data": "FileName", "width": "10%" }, { "data": "FileExtention", "width": "10%" }, { "data": "Id", "render": function (data, type, row, meta) { return `<div class="text-center"> <button class="btn btn-danger text-white" style="cursor:pointer;" type="button" onclick="DeleteRow({data})"> حذف `; }, "width": "15%" } ], "bLengthChange": false, "paging": true, "searching": false, "ordering": true, "language": { "url": "/js/PersianDataTable.json" }, "width": "100%" }); }
function DeleteRow(Id) { swal({ title: "از حذف مطمئن هستید؟", icon: "warning", buttons: true, dangerMode: true }).then((willDelete) => { if (willDelete) { // Send the unique Id to the server for deletion .ajax({ type: "POST", url: "/YourPagePath?handler=Delete", // Update with your actual page path data: JSON.stringify({ id: Id }), contentType: "application/json", headers: { "RequestVerificationToken": ('input[name="__RequestVerificationToken"]').val() }, success: function (response) { if (response.success) { // Remove the row from DataTable var row = dataTable.row(function (idx, data, node) { return data.Id === Id; }); row.remove().draw(); swal("با موفقیت حذف شد!", { icon: "success", }); } else { swal("خطا در حذف!", response.message, "error"); } }, error: function (error) { console.error("Error in AJAX request:", error); swal("خطا در درخواست!", "درخواست حذف با خطا مواجه شد.", "error"); } }); } }); }
- Assistant
Understood! I've reviewed and kept your front-end code in mind. Here's a brief summary to ensure we're on the same page:
-
DataTable Initialization (
loadListFunction):- Fetches the
SampleAnalyzeIdfrom the URL parameters. - Initializes a DataTable (
#DT_load) with columns forFile,FileName,FileExtention, and an action column for deleting rows. - Configured with paging, ordering, and Persian language support.
- Fetches the
-
Row Deletion (
DeleteRowFunction):- Utilizes SweetAlert to confirm deletion.
- Sends an AJAX POST request to delete the specified row on the server.
- Upon successful deletion, removes the row from the DataTable and shows a success message.
- Handles errors by displaying appropriate error messages.
Feel free to ask any questions or request further assistance related to this code!
-