Advertisement
Guest User

datatables show groups instead of entries

a guest
Nov 8th, 2021
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 2.78 KB | None | 0 0
  1. $('#mytable').DataTable({
  2.                             order: [[0, 'desc']],                            
  3.                             rowGroup: {
  4.                                 startRender: function (rows, group, level) {
  5.                                     if (level == 0) {
  6.                                         //need to filter on group because if you do not it will only total what is display on the page
  7.                                         //if group is spilt between a page it will show wrong total for the group.
  8.                                         var total = $('#mytable').DataTable()
  9.                                             .rows()
  10.                                             .data()
  11.                                             .filter(function (data, index) {                                                
  12.                                                 return data[0].display == group ? true : false;
  13.                                             })
  14.                                             .pluck(3)
  15.                                             .reduce(function (a, b) {
  16.                                                 return a + Number(b);
  17.                                             }, 0);
  18.                                         //rows.count() does not total count in group, only with what is shown on page
  19.                                         //so use count of group by filtering.
  20.                                         var count = $('#mytable').DataTable()
  21.                                             .rows()
  22.                                             .data()
  23.                                             .filter(function (data, index) {                                                
  24.                                                 return data[0].display == group ? true : false;
  25.                                             }).count();
  26.  
  27.                                         return $('<tr/>')
  28.                                             .append('<td>' + group + '</td>')
  29.                                             .append('<td class="text-right">' + total.toFixed(2) + '</td>')
  30.                                             .append('<td/>')
  31.                                            
  32.                                             .append('<td>' + count  + '</td>')
  33.                                             .append('<td colspan="6"></td>');
  34.                                     }
  35.                                     return $('<tr/>')
  36.                                         .append('<td colspan="12">' + group + '</td>');
  37.                                    
  38.                                 },
  39.                                 dataSrc: ['0.display', 1, 2]                                
  40.                             },
  41.                             columnDefs: [{
  42.                                 targets: [1,2],
  43.                                 visible: false
  44.                             }]
  45.                         });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement