2021年4月22日星期四

Why when I use drawcallback in datatables it does not work when I display all entries?

I'm using drawcallback to group and show subtotals in the footer of a datatable. It is working but not as I want. When I change the lenght menu to "All", it still show me the result as if I have choosen to show 100 rows. I dont know how to fix it, I want to show 900 rows and the subtotals in the same page. I

        "columnDefs": [{          "visible": false,          "targets": 4      }],      "order": [        [2, 'asc']      ],      "displayLength": 25,      "drawCallback": function (settings) {          var api = this.api();          var rows = api.rows({              page: 'all'          }).nodes();          var last = null;            // Remove the formatting to get integer data for summation          var intVal = function (i) {              return typeof i === 'string' ?              //g global modifier aca cambia el signo peso las barras en cada numero que se suma                i.replace(/[\$,]/g, '.') * 1 :                typeof i === 'number' ?                  i : 0;          };          var groupTotal = {};          api.column(0, {              page: 'all'          }).data().each(function (group, i) {              group_assoc = group.replace(' ', "_");              //console.log(group_assoc);              if (typeof groupTotal[group_assoc] != 'undefined') {                  groupTotal[group_assoc] = groupTotal[group_assoc] + intVal(api.column(8).data()[i]);                  } else {                  groupTotal[group_assoc] = intVal(api.column(8).data()[i]);              }              if (last !== group) {                  $(rows).eq(i).before(                    '<tr class="group"><td colspan="7">' + group + '</td><td class="' + group_assoc + '"></td></tr>'                  );                    last = group;              }          });            var footerText = [];          var footerTotal = [];          for (var key in groupTotal) {              $("." + key).html("$" + groupTotal[key].toFixed(1));              footerText.push(key);  // Get array of group names              footerTotal.push("$" + groupTotal[key].toFixed(1));  // Get array of group totals          }          // Update footer with group names, each on a newline          $(api.column(7).footer()).html(           footerText.join('<br>')         );          // Update footer with group totals, each on a newline          $(api.column(8).footer()).html(           footerTotal.join('<br>')         ); ```  
https://stackoverflow.com/questions/67223007/why-when-i-use-drawcallback-in-datatables-it-does-not-work-when-i-display-all-en April 23, 2021 at 09:57AM

没有评论:

发表评论