Nils
Nils

Reputation: 1996

Find row index for array index in jQuery DataTable

I am using jQuery DataTables to display a paginated table. I now want to jump to the page that contains a certain row (with the current filter/sort settings). If I know the index of the row I can easily do this with something like tbl.fnPageChange( Math.floor(rowi / tbl.fnSettings()._iDisplayLength) );.

However, I only know the index of the data object in the data array (as returned by tbl.fnGetData()).

Is there a simple way to get the row index based on this data index? So pretty much the opposite of fnGetPosition()?

Upvotes: 1

Views: 972

Answers (1)

Nils
Nils

Reputation: 1996

Update: I hacked something together based on looping through the aiDisplay array:

function find_display_index(dataTable, data_index) {
    var oSettings = dataTable.fnSettings();
    var row_index = -1;
    for(var j = 0; j < oSettings.aiDisplay.length; j++) {
        if(oSettings.aiDisplay[j] == data_index) {
            row_index = j;
            break;
        }
    }
    return row_index;
}

Not sure if this is the best (or even totally correct) way, but it seems to work!

Upvotes: 1

Related Questions