fn.dataTable.ext.search.push for only one datatable?

fn.dataTable.ext.search.push for only one datatable?

tekuilatekuila Posts: 24Questions: 5Answers: 0
edited July 2019 in Free community support

Hi.

I use the code "initComplete: function () { .fn.dataTable.ext.search.push etc etc..." (full code below) and it messes up my other datatables.
I already put it inside of var table = $('#datatable').DataTable( {} ) and given the different datatables unique IDs.

How to implement this code for only ONE datatable?

Datatable: http://live.datatables.net/furexita/1/edit (datatables is not being loaded and I don't know why)

initComplete: function () {
  $.fn.dataTable.ext.search.push(
    function( settings, searchData, index, rowData, counter ) {
        // Don't display rows if nothing is checked
        if (filtered.length === 0) {
            return false;
        }

        if (filtered.includes(searchData[10])) {
             return true;
        }
        return false;
    }
  );
}

Thank you
- Jonas

Replies

  • tekuilatekuila Posts: 24Questions: 5Answers: 0
    edited July 2019

    Better to see this datatable (more simple): http://live.datatables.net/yihoxiyi/1/edit

  • colincolin Posts: 15,142Questions: 1Answers: 2,586

    Hi @tekuila ,

    This thread should help, it's asking the same thing.

    Cheers,

    Colin

  • tekuilatekuila Posts: 24Questions: 5Answers: 0
    edited July 2019

    Thanks! I'm inserting the line "if ( settings.nTable.id === 'datatable' ) {" but it makes no difference - one of the other tables are still displaying "No results".
    If I delete the below code then it displays results on the other table.

          // checkbox search
          initComplete: function () {
            $.fn.dataTable.ext.search.push(
              function( settings, searchData, index, rowData, counter ) {
                if ( settings.nTable.id === 'datatable' ) {
                  // Don't display rows if nothing is checked
                  if (filtered.length === 0) {
                      return false;
                  }
    
                  if (filtered.includes(searchData[10])) {
                       return true;
                  }
                  return false;
                }
              }
            );
          }
    

    I'm initializing each datatable like this, but with different IDs.

                $(document).ready(function() {
                  var table = $('#datatable').DataTable( {
    
  • colincolin Posts: 15,142Questions: 1Answers: 2,586

    Hi @tekuila ,

    We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • tekuilatekuila Posts: 24Questions: 5Answers: 0
    edited July 2019

    Thanks, heres a test case: http://live.datatables.net/jatesoda/1/edit

    You can see the line "if (filtered.includes(searchData[6])) {"is removing the results one the second table because there is not a 6th column in the second datatable.

    So the question is, how to make the checkbox code work for ONLY the first table so it doesn't interfere with the second? Thanks.

  • tekuilatekuila Posts: 24Questions: 5Answers: 0

    If I change that line to this:
    if (filtered.includes(settings.searchData[10])) {

    Then all data is loading correctly. But locally the other datatable is stuck on "Processing". Even though the data is not loaded from Ajax.

  • tekuilatekuila Posts: 24Questions: 5Answers: 0

    Getting "TypeError: settings.searchData is undefined"

  • tekuilatekuila Posts: 24Questions: 5Answers: 0

    I think I fixed it!

    By adding the code below:

    if ( settings.nTable.id !== 'datatable' ) { return true; }

    Example here: http://live.datatables.net/jatesoda/2/edit

    Thanks.

  • colincolin Posts: 15,142Questions: 1Answers: 2,586

    Hi @tekuila ,

    There were a few problems with the code - table the wrong size, inconsistent variable naming, etc - but all fixed here. It seems to be doing what you want.

    Hope that helps,

    Cheers,

    Colin

  • pristispristis Posts: 1Questions: 0Answers: 0

    Hey guys!

    Thanks for great examples!

    In this examples, checkboxes filters exact phrase in the cell. Can filter a part of text in the cells? For example I want to filter "Architect" with checkbox and to return System Architect, Interior Architect etc...?

    Thanks for your time!

  • kthorngrenkthorngren Posts: 20,276Questions: 26Answers: 4,765
    edited November 2020

    @pristis Of course :smile: You can use any Javascript comparison method you need. You will want to make it as concise as possible to reduce delays. I took the last example, commented out the comparison and created one the loops all the search terms.
    http://live.datatables.net/yeropoku/46/edit

    Update one of the rows to have Interior Architect. Unchecking all but Architect should show both System and Interior Architect. One it finds a match it uses return true; to display the row.

    Kevin

This discussion has been closed.