Server-Side Processing, Select All, rowCallBack
Server-Side Processing, Select All, rowCallBack
I am using server-side processing to retrieve my data. I want to keep track of selection while going page to page & changing page length. I also want to set a limit to how many rows can be selected across all pages. All this while having the Select All/Deselect All button, & single select.
I am having trouble retaining the selection correctly from page to page as well as changing page length and showing swal (javascript alert) message when desire.
Issues encountered;
1. if selection limit is 10 & 10 rows are selected on the first page, you go to page another page and come back to page 1, the limit logic is entered...this should not happen.
2. if page length is 25 and selection limit is 10, you click select all, 10 rows are selected but when you click deselect all, the deselect logic is ran and it subtracts from the sum amount numbers that are not supposed to be subtracted.
Hopefully what I am saying makes sense. Any advice on logic, how I should track selection, sum amount for each row...anything will help!
here is my code
notClaimedTable
.on( 'select', function ( e, dt, type, indexes ) {
var limitReached = false;
notClaimedTable.rows( indexes ).every( function ( rowIdx, tableLoop, rowLoop ) {
var rowId = notClaimedTable.row(rowIdx).id();
var amountCell = notClaimedTable.cell({ row: rowIdx, column: 4 }).node();
var currentAmount = math.format(math.eval(amountCell.outerText));
if(selectedNotClaimedPaymentIds.length >= claimLimit){
if( $.inArray(rowId, selectedNotClaimedPaymentIds) == -1 ){
$('#'+rowId).removeClass('selected');
}
limitReached = true;
}else {
// if id is not in array, then add
if( $.inArray(rowId, selectedNotClaimedPaymentIds) == -1 ){
recalculateAndPushOnSelect(rowId, currentAmount);
totalRows( selectedNotClaimedPaymentIds.length, sumAmount );
}
}
});
if(limitReached && !hollaBack){
swal(
'Maximum Number of Gifts Reached',
'You can only select up to a max of 1,000 gifts at a time. You must claim the selected gifts first to continue.',
'warning'
);
}
} )
.on( 'deselect', function ( e, dt, type, indexes ) {
// console.log( 'notClaimedTable.on(deselect) | '+ selectedNotClaimedPaymentIds.length+' > '+claimLimit);
console.log('deselect fired');
console.log(selectedNotClaimedPaymentIds.length);
notClaimedTable.rows( indexes ).every( function ( rowIdx, tableLoop, rowLoop ) {
var rowId = notClaimedTable.row(rowIdx).id();
var amountCell = notClaimedTable.cell({ row: rowIdx, column: 4 }).node();
var currentAmount = math.format(math.eval(amountCell.outerText));
sumAmount = math.subtract(sumAmount, currentAmount);
// Remove Id from array
var index = selectedNotClaimedPaymentIds.indexOf(rowId);
if (index > -1) { selectedNotClaimedPaymentIds.splice(index, 1); }
});
totalRows( selectedNotClaimedPaymentIds.length, sumAmount );
// if (typeof selectedNotClaimedPaymentIds !== 'undefined' && selectedNotClaimedPaymentIds !== null) {
// console.log('selectedNotClaimedPaymentIds[]: '+selectedNotClaimedPaymentIds);
// }else{
// console.log('selectedNotClaimedPaymentIds[]: empty');
// }
});
"rowCallback": function( row, data, index ) {
if ( $.inArray(data.Id, selectedNotClaimedPaymentIds) !== -1 ) {
hollaBack = true;
notClaimedTable.row('#'+data.Id).select();
}
if(index == notClaimedTable.page.len()){
hollaBack = false;
}
},