jQuery: Accessing table rows of second and further pages of a datatable

前端 未结 2 668
长发绾君心
长发绾君心 2021-01-14 16:30

I am retrieving table row data like this from a HTML table.

var descriptions = [];

var testRows = $(\'#tbl\').find(\'tbody\').find(\'tr\');
$this = $(this);         


        
相关标签:
2条回答
  • 2021-01-14 17:07

    If you use an older version of jQuery you can use .live() function. With newer version you should switch to .on(). But you can always make a function from what you wrote:

    function example() {
        var testRows = $('#tbl').find('tbody').find('tr');
        $this = $(this);
    
        testRows.each(function () {
            var description = $this.find('[id^="Desc"]').text();
            descriptions.push(description);
        }
    }
    

    and run it every time you operate on your table:

    function removeTr() {
        $('tr').remove();
        example();
    }
    
    0 讨论(0)
  • 2021-01-14 17:09

    I retrieved the data using fnGetNodes API method.

    Correct version (Aug 1 2013)

    var descriptions = [];
    
    var _testDesc;
    var dt = $("tbl").dataTable();
    
    var dtNodes = dt.fnGetNodes;
    var dtNodeCount = dtNodes.length;
    
    for (var i = 0; i < dtNodeCount; i++) {
        var description = $(dtNodes[i].cells[2].innerHTML).val();
        descriptions.push(description);
    }
    

    Wrong version ( Jul 31 2013)

    var descriptions = [];
    
    var _testDesc;
    var dt = $("tbl").dataTable();
    
    var dtElementCollection = dt.DataTable.settings[0].aoData;
    var dtECLength = dtElementCollection.length;
    
    for (var i = 0; i < dtECLength; i++) {
        var description = dtElementCollection[i]._aData[2];
        _testDesc = $(description).val();
        descriptions.push(_testDesc);
    }
    
    0 讨论(0)
提交回复
热议问题