Filter rows by many data-* attributes

Filter rows by many data-* attributes

hotsaucebg3hotsaucebg3 Posts: 5Questions: 1Answers: 0

I have a datatable with table rows like this:
<tr data-cat="First" data-game="Four Kingdoms"></tr>
<tr data-cat="Second" data-game="The Cars"></tr>

And two select fields where I can choose a category / game.
How do I filter the rows by their data-* attributes (the multi filtering must be 'AND' logic)

This question has an accepted answers - jump to answer

Answers

  • sandysandy Posts: 913Questions: 0Answers: 236
    Answer ✓

    Hi hotsaucebg3,

    You can define your own custom search function as described on this page.

    Hope this helps,

    Sandy

  • hotsaucebg3hotsaucebg3 Posts: 5Questions: 1Answers: 0

    Thanks, I've managed to do this, here's the code(it may help someone)

        $.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 ($('#cats_filter').val()) {
                    var category = row.nodes().to$().data('cat');
                    if (category) {
                      if (category.indexOf($('#cats_filter').val()) < 0) {
                        to_return = false;
                      }
                    } else {
                      to_return = false;
                    }
                  }
    
                  //more filters here
    
                  return false; //break each loop
                }
              });
              if (!to_return) {
                return false;
              }
            }
            return true;
          }
        );
    
This discussion has been closed.