Datatables sorting and filtering very slow(non-ajax)

Datatables sorting and filtering very slow(non-ajax)

hotsaucebg3hotsaucebg3 Posts: 5Questions: 1Answers: 0
edited March 2021 in Free community support

I'm using the adminBSB Material design's theme: https://github.com/gurayyarar/AdminBSBMaterialDesign
I have datatable of 254 rows with 6 columns. The sorting is enabled and I have added few filters, and when I sort/click on a filter, it is hella slow, like 3-5 seconds freeze, then load.
Here's my datatables init code:

$(".table_06").dataTable().fnDestroy();
var oTable2 = $('.table_06').DataTable( {
  initComplete: function () {
  }
});

And I have added the filtering code:
// Date range filter
minDateFilter = "";
maxDateFilter = "";
$.fn.dataTableExt.afnFiltering.push(
  function(oSettings, aData, iDataIndex) {
    if ($('.table_06').length) {
      var to_return = true;
      oTable2.rows().every(function () {
        var row = this;
        if (row.index()==iDataIndex) {
          //CATEGORY FILTER
          if ($('#news_cats_filter').val()) {
            var category = row.nodes().to$().data('cat');
            if (category) {
              if (category.indexOf($('#news_cats_filter').val()) < 0) {
                to_return = false;
              }
            } else {
              to_return = false;
            }
          }
          //PAYMENT PROVIDER FILTER
          if ($('#news_paymentprov_filter').val()) {
            var payprov = row.nodes().to$().data('payprov');
            if (payprov) {
              if (payprov.indexOf($('#news_paymentprov_filter').val()) < 0) {
                to_return = false;
              }
            } else {
              to_return = false;
            }
          }
          return false;
        }
      });
      if (!to_return) {
        return false;
      }
    }
    if ($('.table_05').length) {
      if (typeof aData._date == 'undefined') {
        aData._date = new Date(aData[5]).getTime();
      }
      if (minDateFilter && !isNaN(minDateFilter)) {
        if (aData._date < minDateFilter) {
          return false;
        }
      }
      if (maxDateFilter && !isNaN(maxDateFilter)) {
        if (aData._date > maxDateFilter) {
          return false;
        }
      }
    }
    return true;
  }
);

Can the speed of filtering/ordering be improved? I think it loops somewhere since the CPU gets overloaded

Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Replies

  • colincolin Posts: 15,146Questions: 1Answers: 2,587

    Your filter is looping through the rows:

          oTable2.rows().every(function () {
    

    So this means, that for every row, you are filtering through every row, will exponentially slow down with the more rows you add.

    I don't know what that filter is trying to do, but I'd suggest looking at the logic there. I'm unclear why you would need to loop through all the rows - a filter function should only need to look at the row that's being passed to it.

    Colin

  • hotsaucebg3hotsaucebg3 Posts: 5Questions: 1Answers: 0

    I forgot to add, on every row, I have a data-cat attribute, and data-[filter] for every other select(filter)
    When I choose option from the dropdown/select, it should show rows having that data-cat attribute

  • colincolin Posts: 15,146Questions: 1Answers: 2,587

    OK, but instead of searching through all the rows to get the match, you can go straight to that row with:

    oTable2.row(iDataIndex)
    

    Colin

  • hotsaucebg3hotsaucebg3 Posts: 5Questions: 1Answers: 0
    edited March 2021

    problem fixed, just removed the each loop and used
    var category = oTable2.row(dataIndex).nodes().to$().data('cat');

This discussion has been closed.