Get the iDisplayStart of a given row

Get the iDisplayStart of a given row

LanFeusT23LanFeusT23 Posts: 4Questions: 0Answers: 0
edited May 2013 in General
I have a dataTable with hundreds of items with a fixed 50 iDisplayLength option. I need to be able to find what page a specific row is within the loaded nodes.

All I've managed is to get the position, unfortunately that internal row position does not correspond to the row index with the current sorting and filtering.

As an example here on http://jsfiddle.net/VFtQQ/. I can retrieve the position or row #tr4 (position 3) but what I need is the index of the sorted row.

fnGetNodes allows me to get all the nodes as they were passed from the server, is there a way for me to get the nodes with the current sorting options?

Currently my only way would be to be sure that the datatable sorting is the same as my server side array and do the pagination math on the server side.

Replies

  • LanFeusT23LanFeusT23 Posts: 4Questions: 0Answers: 0
    edited May 2013
    I ended up writting a small plugin for it:
    [code]

    // get the page of a given item in order to paginate to it's page on load
    $.fn.dataTableExt.oApi.fnGetPageOfRow = function (oSettings, iRow) {
    // get the displayLength being used
    var displayLength = oSettings._iDisplayLength;

    // get the array of nodes, sorted (default) and using current filters in place for all pages (default)
    // see http://datatables.net/docs/DataTables/1.9.beta.1/DataTable.html#%24_details for more detals
    var taskListItems = this.$("tr", { "filter": "applied" });

    // if there's more than one page continue, else do nothing
    if (taskListItems.length <= displayLength) return;

    // get the index of the row inside that sorted/filtered array
    var index = taskListItems.index(iRow);

    // get the page by removing the decimals
    var page = Math.floor(index / displayLength);

    // paginate to that page
    this.fnPageChange(page);
    };
    [/code]
  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    Very nice! Thanks for sharing this with us. That looks really useful, particularly when combined with something like fnAddData .

    Regards,
    Allan
This discussion has been closed.