Can I use DataTables only to show filtered stats, without updating the table?

Can I use DataTables only to show filtered stats, without updating the table?

veryacaveryaca Posts: 11Questions: 4Answers: 0

Let me explain what I"m trying to achieve.

I have this large table, and I would like to have a section on the same page where I can see some stats related to this table, without actually filtering it.

So say I have 4,000 lines. I would like to have a list that says how many lines have Monday, how many have Tuesday, etc... without actually applying the filter to the table?

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947

    You can use filter() for this. The docs have an example. You can check the length of the resulting data to display the count. Its funny, I just answered another question (different need) with an example that you might find useful.

    See the example I posted in this thread.

    Kevin

  • veryacaveryaca Posts: 11Questions: 4Answers: 0
    edited December 2018

    I don't want the table to update. I just want to be able to grab stats from the table once it's loaded.

    So say I have 4,000 rows. Once the DataTable has loaded, I want to show in a list somewhere on the same page, that 1,800 rows have the value Monday (for instance) in a specific column (without filtering the table).

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947

    The filter() API does not update the table. It returns a data set that can be used in Javascript. Per the docs:

    The filter() method provides a way of filtering out content in an API instance's result set which does not pass the criteria set by the provided callback method. This method should not be confused with search() which is used to search for records in the DataTable - i.e. the filter method does not change the rows that are displayed in the DataTable.

    Take a closer look at my example and the example in the docs.

    Kevin

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947
    edited December 2018 Answer ✓

    Also after filtering you can use count() to get the number of items returned. Just replace toArray() from my example with count().

    EDIT: I updated my example for you:
    http://live.datatables.net/nalotira/2/edit

    Kevin

  • veryacaveryaca Posts: 11Questions: 4Answers: 0
    edited December 2018

    I was about to ask, haha! So say I have this code.

    var filteredData = table.column(1).data().filter( function ( value, index ) {
        return value == 'NBA' ? true : false;
    });
    

    I'm not sure how I would include the count() to this? I want to know how much rows have the value 'NBA' in column 1.

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947

    We probably cross posted. My last post has an example.

    Kevin

  • veryacaveryaca Posts: 11Questions: 4Answers: 0

    Thank you so much for your help!

This discussion has been closed.