jQuery Datatables, Bulk adding rows with additional attributes

jQuery Datatables, Bulk adding rows with additional attributes

monkeytoothmonkeytooth Posts: 22Questions: 0Answers: 0
edited April 2013 in DataTables 1.9
Currently I am adding rows to a datatable like:

[code]$.each(data.data.events, function(i, v)
{
var tobj = data.data.events[i];
var dateString = formatStatusDate(tobj.event_ts_begin);
statusMessageTable.fnAddData([
tobj.severity_icon,
tobj.event_severity,
tobj.type_icon,
tobj.event_code,
tobj.event_desc,
tobj.event_type,
tobj.event_text,
dateString
]);
var theNode = statusMessageTable.fnSettings().aoData[i].nTr;
theNode.setAttribute('data-idn',tobj.event_id);
});[/code]
However I have noticed this causes a great strain on the memory if there are more than 100 rows to add. Now I know many of you the first thought is, well why don't you render it via server side script, then apply your datatable logic. To that I say I can't to much of the page is dynamically generated by JS alone, as its mostly a javascript intense application.

So with that I am been trying to figure out a way to add all the rows I need down the line, while retaining the ability to add the attribute like I have being done. I am hoping someone more proficient than me with datatables may be able to help me figure something out, or offer some idea to what I can do to build out a object of sorts that can if possible be passed to the fnAddData function that will allow me at the same time to add that attribute to the parent row being added.

Replies

  • allanallan Posts: 63,180Questions: 1Answers: 10,411 Site admin
    fnAddData does a draw by default. You most certainly want to disable that if you are calling fnAddData more than once - otherwise it will to a resort and refilter evert time, killing the CPU.

    Allan
  • monkeytoothmonkeytooth Posts: 22Questions: 0Answers: 0
    If the table is already instantiated, and I am adding rows as described above after its instantiated. Is there a way I can disable the draw, then at the end of the loop reenable it? I have tried creating the table by means of having javascript build all the HTML then instantiate datatables after its built, and I was losing functionality on the tables. Whatever I was doing datatables ultimately didn't like. Which is why I went to the fnAddData route. Cause datatables retained its functionality, and actually all around better than expected. With exception of this case. Where I could be adding sooo many rows that this is killing the memory. I unfortunately need to make sure that the TR itself gets that attribute for other scripts to work properly though.

    I played with the idea of having the attribute added as a hidden column and pulling the attribute that way (or even itterating over the table and adding it afterward. Which sped it up some, but still that draw your talking about I think slows it down memory wise, nothing with your code, its all mine. But still lol, anyway to turn it off then back on?
This discussion has been closed.