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

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

CaptainBettyCaptainBetty Posts: 3Questions: 1Answers: 0
edited April 12 in Free community support

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

  • kthorngrenkthorngren Posts: 20,346Questions: 26Answers: 4,776
    edited April 12

    var thisHeaderKey = row.data[0];

    My guess is the errors are with this statement. The row parameter is the row/s node not data. See the createdRow docs for details. Instead you probably will want to use the data parameter, like this:

    var thisHeaderKey = data[0];
    

    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

  • CaptainBettyCaptainBetty Posts: 3Questions: 1Answers: 0

    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?

  • kthorngrenkthorngren Posts: 20,346Questions: 26Answers: 4,776

    Using previousRow.data()[0] works in this test case:
    https://live.datatables.net/xenuvuma/1/edit

    but it's not an array (although it looks like an array).

    That seems odd, unless you are using columns.data but then I would suspect that var 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.

    get all kinds of fun errors.

    What errors are you getting?

    Kevin

  • CaptainBettyCaptainBetty Posts: 3Questions: 1Answers: 0

    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.

  • kthorngrenkthorngren Posts: 20,346Questions: 26Answers: 4,776

    This returns what I'm expecting although I do not like the way I have to get at it.

    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

Sign In or Register to comment.