Animesh D
Animesh D

Reputation: 5002

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

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

var descriptions = [];

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

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

Since this table is a datatable which has a page limit of 5 elements, I can only access data in the first 5 rows.

I am aware that paginated data is removed from DOM, so that would be the reason why I can't access rest of the rows.

How do I go beyond first page and access remaining rows data?

Upvotes: 3

Views: 3363

Answers (2)

Animesh D
Animesh D

Reputation: 5002

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);
}

Upvotes: 1

Kamil Szymański
Kamil Szymański

Reputation: 950

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();
}

Upvotes: 0

Related Questions