jquery DataTables. How to get filtered (visible) rows

后端 未结 7 1350
盖世英雄少女心
盖世英雄少女心 2021-02-07 02:28

I have a button that apply filter to jquery datatable

$(\"#buttonFilter\").button().click(function() {
                if (lboxColor.val() != null) {
                    


        
相关标签:
7条回答
  • 2021-02-07 02:58

    For those who are interested, this is a concrete usecase.

    /**
     * Select all the elements of the datatable which match the current user
     * search (or all if no search).
     */
    function dtable_selectAll()
    {
        idTable = 'myDataTable';
        var rows = $('#' + idTable).dataTable()
                .$('tr', {"filter":"applied"});
        var oTT = TableTools.fnGetInstance(idTable);
        $(rows).each(function (index, el){
            oTT.fnSelect(el);
        })
    }
    

    Hope it helps.

    0 讨论(0)
  • 2021-02-07 03:05

    For those struggling to get data from only one column AND only from visible rows: you can actually combine the "selectors" like this

    var total = api
          .column(2, { search: 'applied' }) // <-- only 3rd column && only visible after applied search
          .data()
          .reduce(function (a, b) {
            return floatVal(a) + floatVal(b)
          }, 0)
    

    I also notices several sources stating { page: 'current' } would be the right selector for currently visible rows, but this one actually gives you the rows visible after a possible pagination.

    0 讨论(0)
  • 2021-02-07 03:07

    Just for completeness, and as Yuri mentioned, there are "plugins" that will provide the filtered TR Nodes or data.

    To use these plugins, you will need to paste the code from those links into your script file.

    0 讨论(0)
  • 2021-02-07 03:09

    Just in case you want a collection of nodes (DOM elements) just like fngetNodes(), you can use the '$' instead of the '_' like this table.$('tr', {"filter":"applied"});

    the '_' returns a collection of 'TR' (html elements).

    0 讨论(0)
  • 2021-02-07 03:11

    I've been searching for about an hour, for anyone using DataTables 1.10+ if you want to get the filtered (better term: "searched") rows:

    var table = $('.table').DataTable({...});
    
    function selectOnlyFiltered(){
       var filteredRows = table.rows({filter: 'applied'});
    }
    
    0 讨论(0)
  • 2021-02-07 03:11

    For datatables 1.9 and later this solution works:

    myDataTableHandle = $('#example1').dataTable(...);
    ...
    ...
    var myFilteredRows = myDataTableHandle._('tr', {"filter":"applied"});
    

    and you won't have to include a separate api plugin. :)

    0 讨论(0)
提交回复
热议问题