Retrieving row data after filtering JQuery Datatables

后端 未结 8 403
广开言路
广开言路 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:52

    Thanks AlecBoutin, that's the easiest way.
    I am trying to search into multiple tables arranged in tabs, and I want to display the table where a result is found. Quite easy with your solution

        // make the global search input search into all tables (thanks to a class selector)
        $('#oversearch').on( 'keyup', function () {
            $('.table').DataTable().search( this.value ).draw();
    
            var row = $('.table').DataTable().$('tr', { "filter": "applied" });
            console.log(row.parents("div")[1]);
        });
    

    you can then navigate into whatever parent you need with the parents() jquery. (here I'm choosing the 2nd div parent encountered)

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

    You can get the visible rows list changing the fnGetHiddenTrNodes function as follows.

     $.fn.dataTableExt.oApi.fnGetVisibleTrNodes = 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);
                var visibleNodes = [];
    
                for (var i = 0; i < anDisplay.length; i++) {
                    var iIndex = jQuery.inArray(anDisplay[i], anNodes);
    
                    if (iIndex != -1) {
                        visibleNodes.push(anDisplay[i]);
                    }
                }
    
                /* Fire back the array to the caller */
                return visibleNodes;
            }
    
    0 讨论(0)
提交回复
热议问题