Retrieving row data after filtering JQuery Datatables

后端 未结 8 402
广开言路
广开言路 2020-12-28 18:09

Seems like it should be easy but...

Does anyone know how to return the current rows from a filtered dataTable? The oTable.fnGetNodes() method returns

相关标签:
8条回答
  • 2020-12-28 18:36

    If you're trying to get the actual tr DOM elements instead of the data, the solution is similar to the underscore solutions provided above, but you use the $ method instead.

    function getFilteredDatatable() {
        return $("table.dataTable").dataTable().$('tr', { "filter": "applied" });
    }
    

    More information is available on the API documentation page. http://datatables.net/api

    0 讨论(0)
  • 2020-12-28 18:36

    Having the table setup this worked for me having a checkbox with the id for each row and another check box that will select all rows either when a filtered has been applied(just filtered rows) or not (all rows)

    $(document).ready(function() {
        var myDataTableHandle = $('#example').dataTable({"bPaginate": false});
    
       $('#selectAllCheckBox').click(function() {
           if($(this).is(':checked')){ 
              var filteredRows  =   myDataTableHandle._('tr', {"filter":"applied"});
              alert( filteredRows.length +' nodes were returned' );     
              $(myDataTableHandle.fnGetNodes()).find($('input[name=idCheckBox]')).each(function () {
                  $(this).prop('checked', true);
               });
            else{ 
              $('input[name=idCheckBox]:checked').prop('checked', false);
             } 
          });
    });
    
    0 讨论(0)
  • 2020-12-28 18:37

    As of Datatables 1.10, there is a built-in way to get the filtered or unfiltered rows after a search.

    var table = $('#example').DataTable();
    table.rows( {search:'applied'} ).nodes();
    table.rows( {search:'removed'} ).nodes();
    

    There are other options for getting only the current page or all pages as well as the order. More details here: http://datatables.net/reference/type/selector-modifier

    0 讨论(0)
  • 2020-12-28 18:41

    Better late than never but I was struggling with this myself. Here's what I came up with

    $.fn.dataTableExt.oApi.fnGetVisibleData = function(){
        displayed = [];
        currentlyDisplayed = this.fnSettings().aiDisplay; //gets displayed rows by their int identifier
        for (index in currentlyDisplayed){
            displayed.push( this.fnGetData( currentlyDisplayed[index] ));
        }
        return displayed;
    
    }
    
    0 讨论(0)
  • 2020-12-28 18:42

    The easiest way to do this is actually built right in to the DataTables API:

    _('tr', {"filter": "applied"})
    

    Used in a Function:

    function get_filtered_datatable() {
        var filteredrows = $("#mydatatable").dataTable()._('tr', {"filter": "applied"});
    
        for ( var i = 0; i < filteredrows.length; i++ ) {
            debug.console(filteredrows[i]);
        };
    }
    
    0 讨论(0)
  • 2020-12-28 18:52

    Figured out the answer, if anyone ever needs this:

    First, using this datatables extension to get all the hidden rows:

    $.fn.dataTableExt.oApi.fnGetHiddenTrNodes = function (oSettings, arg1, arg2) {
    
    /* Note the use of a DataTables 'private' function thought the 'oApi' object */
    
    var anNodes = this.oApi._fnGetTrNodes(oSettings);
    var anDisplay = $('tbody tr', oSettings.nTable);
    
    /* Remove nodes which are being displayed */
    for (var i = 0; i < anDisplay.length; i++) {
        var iIndex = jQuery.inArray(anDisplay[i], anNodes);
    
        if (iIndex != -1) {
            anNodes.splice(iIndex, 1);
        }
    }
    
    /* Fire back the array to the caller */
    return anNodes;
    }
    

    Then filter out the hidden nodes to get only visible nodes:

     var rows = oTable.fnGetNodes(); // get all nodes            
     var rows_hidden = oTable.fnGetHiddenTrNodes(); // get all hidden nodes
    
     var result = [], found;
    
     // remove hidden nodes from all nodes
     for (var i = 0; i < rows.length; i++) {
      found = false;
        for (var j = 0; j < rows_hidden.length; j++) {
          if (rows[i] == rows_hidden[j]) {
            found = true;
              break;
                    }
                }
                if (!found) {
                    result.push(rows[i]); 
                }
        }
    
    0 讨论(0)
提交回复
热议问题