Searching Across Multiple Tables

Searching Across Multiple Tables

tpurchertpurcher Posts: 2Questions: 2Answers: 0

Hello.

I have multiple DataTables on the same page. I have one search box and I need to be able to search across all the tables simultaneously.

This page (https://www.datatables.net/examples/basic_init/multiple_tables.html) indicates, "Additionally, the API can be used to manipulate both together, or independently", bu no examples are given.

Here is how I create my DTs. There are several HTML tables that have the 'table-data' class.

  var tables = $('.table-data').DataTable({
            "bPaginate": false,
            
            "columnDefs": [
                { 'orderable': false, 'targets': [2, 3, 4, 5, 6, 7, 8, 9, 10, 11] },
                {
                   "targets": 2,
                   "render": function (data, type, row) {
                      uniqueShareClasses[data] = data;
                      return data;
                   }
                },
            ],
            "info": false,
            "order": [[0, "asc"], [1, "asc"]]
         });

In my search function I have this code. My input box has an id of 'search'.

 tables.search($('#search').val()).draw();

I seem to be able to search in the first table only. Non of the remaining tables are searched. Are there any examples of this?

Answers

  • bradbamfordbradbamford Posts: 1Questions: 0Answers: 0

    I'm working on the same thing. I got search to work accross all tables, but I can't figure out how to disabled per table search so that only my universal search box works.

    Here is what I used for universal search:

    $('table').DataTable({"searching":true});
    
    function filterGlobal() {
      $('table').DataTable().search(
        $('#global_filter').val()
      ).draw();
    }
    
    $('#global_filter').on('keyup click', function () {
      filterGlobal();
    });
    

    Now, if anyone can help me disabled the also included individual table search fields, that would be great.

  • allanallan Posts: 63,552Questions: 1Answers: 10,477 Site admin

    Use:

    $('table.dataTable').DataTable().search( ... ).draw();
    

    to search all DataTables. Or:

    $.fn.dataTable.tables( { api: true } ).search( ... ).draw();
    

    The key is to make sure that your API instance selects multiple DataTables.

    Allan

  • allanallan Posts: 63,552Questions: 1Answers: 10,477 Site admin

    Now, if anyone can help me disabled the also included individual table search fields, that would be great.

    Use the dom option and remove the f option.

    Allan

This discussion has been closed.