refactor datatables

This commit is contained in:
bobbyvish
2024-03-26 13:22:29 +05:30
parent 64c172ab59
commit 2a4f507a6f
22 changed files with 1191 additions and 391 deletions

View File

@@ -40,18 +40,20 @@
<thead>
<tr role="row">
<th class="checkbox-column sorting_asc text-center" tabindex="0"
<th class="checkbox-column dt-no-sorting text-center" tabindex="0"
aria-controls="style-3" aria-sort="ascending" style="width: 50.2656px;">
#</th>
<th class="sorting_asc text-center" tabindex="0" aria-controls="style-3"
aria-sort="ascending" style="width: 50.2656px;">#</th>
aria-sort="ascending" style="width: 50.2656px;">Record Id</th>
<th class="sorting text-center" tabindex="1" aria-controls="style-3"
colspan="1" style="width: 44.2344px;">Title</th>
<th class="sorting text-center" tabindex="2" aria-controls="style-3"
colspan="1" style="width: 44.2344px;">Message</th>
<th class="sorting text-center" tabindex="2" aria-controls="style-3"
colspan="1" style="width: 44.2344px;">Created on</th>
<th class="sorting text-center" tabindex="5" aria-controls="style-3"
style="width: 79.7969px;">Active</th>
<th class="sorting text-center" tabindex="6" aria-controls="style-3"
<th class="text-center dt-no-sorting" tabindex="6" aria-controls="style-3"
style="width: 79.7969px;">Action</th>
</tr>
</thead>
@@ -86,7 +88,7 @@ var dataTableInstance
var actionUrl = '{% url "module_notification:notification_action" %}'
var mainUrl = '{% url "module_notification:notification_list" %}?deleted_flag=False';
var editUrl = "{% url 'module_notification:notification_edit' pk=0 %}"
var viewArchiveUrl = "{% url 'module_notification:notification_action' %}"
var viewArchiveUrl = "{% url 'module_notification:notification_archive' %}"
var notifyUrl = "{% url 'module_notification:notification_send' %}"
// Entry point
@@ -111,6 +113,7 @@ function initializeDataTable(tableName, mainUrl) {
{ data: "id", className: "text-center" },
{ data: "title", className: "text-center" },
{ data: "message", className: "text-center" },
{ data: "timestamp", className: "text-center" },
{ data: "active", className: "text-center", render: renderSwitch },
{ data: null, className: "text-center", render: renderActions }
],
@@ -203,6 +206,11 @@ function initCompleteCallback() {
}
function HideButton(className) {
// Hide archive button
$(`.${className}`).hide();
}
// Function to handle archive action
function archiveAction() {
// Get all the checked checkboxes
@@ -210,8 +218,8 @@ function archiveAction() {
// If no checkboxes are checked, show an error message
if (checkedCheckboxes.length === 0) {
Swal.fire({
title: 'No users selected',
text: 'Please select at least one user to archive.',
title: 'No record selected',
text: 'Please select at least one record to archive.',
icon: 'error',
showConfirmButton: true
});
@@ -249,16 +257,17 @@ function archiveAction() {
icon: 'success',
showConfirmButton: true
});
HideButton("buttons-archive");
// Optionally, you can reload the DataTable after successful archive
reloadDataTable();
},
error: function(response) {
// Show error message
error: function(jqXHR, textStatus, errorThrown) {
var response = JSON.parse(jqXHR.responseText);
Swal.fire({
title: 'Error!',
text: response.message,
icon: 'error',
showConfirmButton: true
title: 'Error!',
text: response.message,
icon: 'error',
showConfirmButton: true
});
}
});
@@ -294,13 +303,13 @@ function activeSwitchEventListener() {
// Reload the DataTable after successful toggle
reloadDataTable();
},
error: function(response) {
// Show error message
error: function(jqXHR, textStatus, errorThrown) {
var response = JSON.parse(jqXHR.responseText);
Swal.fire({
title: 'Error!',
text: response.message,
icon: 'error',
showConfirmButton: true
title: 'Error!',
text: response.message,
icon: 'error',
showConfirmButton: true
});
}
});
@@ -311,8 +320,6 @@ function sendNotificationAction() {
$('body').on('click', '.send', function() {
var id = $(this).closest('tr').find('.send').data('id');
console.log("=================================================id is", + id);
Swal.fire({
title: 'Send Notification?',
text: 'Once sent, the notification will be delivered to the user.',
@@ -341,13 +348,13 @@ function sendNotificationAction() {
});
// Optionally, you can perform any other actions after sending the notification
},
error: function(response) {
// Show error message
error: function(jqXHR, textStatus, errorThrown) {
var response = JSON.parse(jqXHR.responseText);
Swal.fire({
title: 'Error!',
text: response.message,
icon: 'error',
showConfirmButton: true
title: 'Error!',
text: response.message,
icon: 'error',
showConfirmButton: true
});
}
});