skip cache, excludes a server side datatables

This commit is contained in:
David Brazda
2023-11-24 17:59:46 +01:00
parent 576745d8f9
commit e31f44bb8e
9 changed files with 1417 additions and 14 deletions

View File

@@ -919,11 +919,16 @@ $("#delModalArchive").on('submit','#delFormArchive', function(event){
var archiveRecords =
$('#archiveTable').DataTable( {
ajax: {
url: '/archived_runners/',
dataSrc: '',
url: '/archived_runners_p/',
// dataSrc: 'data',
beforeSend: function (xhr) {
xhr.setRequestHeader('X-API-Key',
API_KEY); },
// data: function(d) {
// d.start = d.start;
// d.length = d.length;
// d.draw = d.draw;
// },
error: function(xhr, status, error) {
//var err = eval("(" + xhr.responseText + ")");
//window.alert(JSON.stringify(xhr));
@@ -950,7 +955,8 @@ var archiveRecords =
{data: 'batch_id', visible: true},
],
paging: false,
processing: false,
processing: true,
serverSide: true,
columnDefs: [{
targets: [0,1,17],
render: function ( data, type, row ) {
@@ -1104,8 +1110,9 @@ var archiveRecords =
],
order: [[6, 'desc']],
select: {
info: true,
style: 'multi',
selector: 'td'
selector: 'tbody > tr:not(.group-header) td'
},
paging: true,
// lengthChange: false,
@@ -1116,8 +1123,104 @@ var archiveRecords =
// $(row).addClass('highlight');
// }
//}
} );
// Add row grouping based on 'batch_id'
rowGroup: {
dataSrc: 'batch_id',
startRender: function (rows, group) {
var groupId = group ? group : 'no-batch-id';
// Initialize variables for the group
var itemCount = 0;
var firstNote = '';
var profit = '';
// Process each item only once
archiveRecords.rows({ search: 'applied' }).every(function (rowIdx, tableLoop, rowLoop) {
var data = this.data();
if ((group && data.batch_id === group)) {
itemCount++;
if (itemCount === 1) {
firstNote = data.note ? data.note.substring(0, 14) : '';
try {
profit = data.metrics.profit.batch_sum_profit;
} catch (e) {
profit = 'N/A';
}
}
}
});
// Construct the group header
var groupHeaderContent = '<strong>' + (group ? 'Batch ID: ' + group : 'No Batch') + '</strong>';
groupHeaderContent += (group ? ' <span>(' + itemCount + ')</span>' : '');
if (firstNote) {
groupHeaderContent += ' ' + firstNote;
}
if (profit) {
groupHeaderContent += ' - <span class="profit-info">Profit: ' + profit + '</span>';
}
return $('<tr/>')
.append('<td colspan="18">' + groupHeaderContent + '</td>')
.attr('data-name', groupId)
.addClass('group-header collapsed');
}
},
drawCallback: function (settings) {
var api = this.api();
var rows = api.rows({ page: 'current' }).nodes();
// Iterate over all rows in the current page
api.column(17, { page: 'current' }).data().each(function (group, i) {
var groupName = group ? group : 'no-batch-id';
var stateKey = 'dt-group-state-' + groupName;
var state = localStorage.getItem(stateKey);
if (state === 'collapsed') {
// Hide all rows in the collapsed group
$(rows).eq(i).hide();
$('tr[data-name="' + groupName + '"]').addClass('collapsed');
} else {
// Show all rows in the expanded group
$(rows).eq(i).show();
$('tr[data-name="' + groupName + '"]').removeClass('collapsed');
}
});
}
});
// Function to generate a unique key for localStorage based on batch_id
function generateStorageKey(batchId) {
return 'dt-group-state-' + batchId;
}
// Expand/Collapse functionality
$('#archiveTable tbody').on('click', 'tr.group-header', function () {
var name = $(this).data('name');
var collapsed = $(this).hasClass('collapsed');
$(this).toggleClass('collapsed');
archiveRecords.rows().every(function () {
var rowGroup = this.data().batch_id ? this.data().batch_id : 'no-batch-id';
if (rowGroup === name) {
if (collapsed) {
this.node().style.display = '';
} else {
this.node().style.display = 'none';
}
}
});
// Save the state
if (collapsed) {
localStorage.setItem(generateStorageKey(name), 'expanded');
} else {
localStorage.setItem(generateStorageKey(name), 'collapsed');
}
});
//WIP buttons to hide datatable columns
// document.querySelectorAll('a.toggle-vis').forEach((el) => {
// el.addEventListener('click', function (e) {

File diff suppressed because it is too large Load Diff