Cannot seem to access data by index of previous row using createdRow
Cannot seem to access data by index of previous row using createdRow

I'm trying to compare rows for the same data in the first column.
Using createdRow I'm getting the data of the first column of the row; that's easy. It works via data parameter: data[0].
Now I need to get the previous row and it's first column data.
I can get the previous row. log out previousRow's data works fine. It appears to be an array. But when I go to access the first column by index: previousRow.data()[0] or previousRow.data[0] I get all kinds of fun errors.
What am I doing wrong?
Here's my function:
createdRow: function (row, data, dataIndex, cells) {
var t = $(this).DataTable();
var thisHeaderKey = row.data[0];
if (dataIndex > 0) {
var previousRow = t.row(dataIndex - 1);
if (previousRow && previousRow.data()[0] === thisHeaderKey) {
// do stuff to rows
}
}
Answers
My guess is the errors are with this statement. The
row
parameter is the row/s node not data. See thecreatedRow
docs for details. Instead you probably will want to use thedata
parameter, like this:if you still need help please provide a simple test case replicating the issue so we can help debug.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
My copied code was messed up. Sorry.
var headerKey = data[0];
this works as expected as it's the data of the current row.
My issue is getting the same column data from the **previous ** row.
I can get the data, but it's not an array (although it looks like an array)... i can loop over the results but directly accessing it by column index isn't working.
Suggestions?
Using
previousRow.data()[0]
works in this test case:https://live.datatables.net/xenuvuma/1/edit
That seems odd, unless you are using
columns.data
but then I would suspect thatvar headerKey = data[0];
wouldn't work. Maybe you can post an example of one of the rows? OR better update the test case to show the problem you are having.What errors are you getting?
Kevin
I guess I figured out a way to get at the values I'm expecting.
Object.values(previousRowData)[0]
This returns what I'm expecting although I do not like the way I have to get at it.
Thanks for taking a look.
Are you using
columns.data
?Post your full Datatables initialization code so we can see what you have. Or provide a simple test case.
Kevin