Search code examples
javascriptjquerydatatables

Datatables createdRow function not setting id to row element


"createdRow": function (row, data, dataIndex) {
    var a = dataIndex;
    alert(a);
    var i = policyInfoArray[dataIndex].id;
    alert(i);

    $('td', row).eq(0).attr('id', i);
}

When I run this code, alert(a) pops up correctly and alert(i) gives me the correct result as well. but for some reason

 $('td', row).eq(0).attr('id', i);

gives me an error when I put i in there. However this:

 $('td', row).eq(0).attr('id', 5);

works fine. Can someone please direct me as to what I am doing wrong?


Solution

  • It looks like your code is setting the id to the first td element of the table row. If you change the last line of your function to this, it will add the id to the row element itself:

    $(row).eq(0).attr('id', i);
    

    It is likely that the problem you see is to do with the data in your policyInfoArray array.

    See working demo here.