Adding row attributes after fnAddData?
Adding row attributes after fnAddData?
Is there any easy way to add attributes to a row newly added with fnAddData? I'd like each row to have a unique ID. Originally, I've just been adding the row HTML myself manually into the DOM. But this obviously doesn't get added to DataTables then which makes that row disappear from the page once you sort/change pages/filter.
So is there any way to add an ID to my new rows with or after fnAddData? Or a way to make DataTables pick up a new row added with jQuery's basic .append()?
Thanks.
So is there any way to add an ID to my new rows with or after fnAddData? Or a way to make DataTables pick up a new row added with jQuery's basic .append()?
Thanks.
This discussion has been closed.
Replies
[code]
function getRowData(p_id)
{
var settings = oTable.fnSettings();
for (var i=0; i
fnAddData returns an array of indexes which point to the data storage point in the aoData object where DataTables stores the row which has just been added.
So for example:
[code]
/* assume we have an array of data called 'a' that we want to add to a table 'oTable' */
var ai = oTable.fnAddData( a );
var n = oTable.fnSettings().aoData[ ai[0] ].nTr;
/* n is now the TR that you added - so it can be modified */
[/code]
Not the most gorgeous code in the world - but does the trick :-). I plan to write a plug-in API function in the near future which will take an existing TR node (perhaps one which you have constructed using DOM methods) and be able to add that. This would retain the ID etc - but until then - this extra line will give you the node you added :-)
Regards,
Allan
Hmm, I'll have to think something up. Thanks for the response.
http://datatables.net/api
@Razputin: Yup to update a row or cell, you need to use fnUpdate() - that way filtering and sorting will take the change into account.
Allan
One thing to remember is that DataTables removes nodes from the DOM when they are not on display (ie. if pagination is enabled). As such, if you add a row with an id of 'allan' and it isn't displayed on that page, you couldn't do 'document.getElementById('allan')! You would need to use fnGetNodes() which gets all nodes, and filter them down. $('#allan', oTable.fnGetNodes()); would do it.
Allan
And yeah, that's why I realize now that it's a bad way to even do it (because of the row not being on display). But thanks for that fnGetNodes suggestion, I'll definitely try that too! Easy enough updates.
EDIT: Err, disregard that. ".not()" is obviously not what we want either, as that would remove #allan.
I've been wondering about how sensible it would be to integrate some kind of selector into DataTables to allow for this kind of thing a little easier. Something that I'll look at...
Allan
provided that the id is unique I guess, which should be if we're following W3 guidelines.