Finding position of a row in the table
Finding position of a row in the table
alhenderson
Posts: 9Questions: 0Answers: 0
I have a table whereby I have a hidden column to control the sort order. I need to be able to identify where in the table a given row is, but fnGetPosition isn't working for me.
Say I have 3 rows in the table, and I add another one, but set its sort data such that it is actually the 2nd in the table. If I call fnGetPosition on that new row it returns 3 rather than 1 (presumably because it was added to the end of the table before sorting). What I need to do is work upwards through the DOM from the selected row (has a 'selected' class applied, so easy to identify) to identify the next row up that has a given property. I can't use standard jQuery prevAll because it won't cover multiple pages in the table.
I thought that the '$' API call might do it but I can't see how at the moment.
Anyone got any ideas?
Thanks,
Al.
Say I have 3 rows in the table, and I add another one, but set its sort data such that it is actually the 2nd in the table. If I call fnGetPosition on that new row it returns 3 rather than 1 (presumably because it was added to the end of the table before sorting). What I need to do is work upwards through the DOM from the selected row (has a 'selected' class applied, so easy to identify) to identify the next row up that has a given property. I can't use standard jQuery prevAll because it won't cover multiple pages in the table.
I thought that the '$' API call might do it but I can't see how at the moment.
Anyone got any ideas?
Thanks,
Al.
This discussion has been closed.
Replies
function GetRowIndexInTable(szTableID, oTR)
{
var oTable = $("#"+szTableID).dataTable();
//
// Get all rows in the table (regardless of paging etc) in their current order
//
var arrRows = oTable.$("tr");
var iIdx = 0;
var iRowIdx = -1;
var bFound = false;
//
// Look through them all, checking to see which matches the row we've been
// given.
//
while((iIdx < arrRows.length) && !bFound)
{
//
// arrRows has DOM objects in it, oTR is a jQuery row, so we need to get
// a DOM row from that.
//
if(oTR.get(0) === arrRows[iIdx])
{
bFound = true;
iRowIdx = iIdx;
}
iIdx++;
}
return iRowIdx;
}