Dynamically changing cell content
Dynamically changing cell content
Hi Allan
I am investigating the best way to dynamically change data in a particular cell. I think the correct approach is to use fnUpdate which I understand is the api to allow manipulation of the underlying data.
[code]
var iCol = findColumnNumber('membershipStatusText', $('#programtable').dataTable().fnSettings().aoColumns);
$('#programtable').dataTable().fnUpdate('new value', 0, iCol, false); // false because I don't want to redraw the table
[/code]
I would like to pass the 2nd argument to fnUpdate as the aoData index as per the docs. My question is, how do I find what the index is where I have a large (1-2k records) json array?
Alternatively, is there a better approach?
Thanks
I am investigating the best way to dynamically change data in a particular cell. I think the correct approach is to use fnUpdate which I understand is the api to allow manipulation of the underlying data.
[code]
var iCol = findColumnNumber('membershipStatusText', $('#programtable').dataTable().fnSettings().aoColumns);
$('#programtable').dataTable().fnUpdate('new value', 0, iCol, false); // false because I don't want to redraw the table
[/code]
I would like to pass the 2nd argument to fnUpdate as the aoData index as per the docs. My question is, how do I find what the index is where I have a large (1-2k records) json array?
Alternatively, is there a better approach?
Thanks
This discussion has been closed.
Replies
Use fnGetPosition to get the data index from a row. However, I would recommend against using fnGetPosition - it has no benefit (performance wise) and only serves to confuse (it is deprecated in 1.10).
So the real question is, how do you know which row you want to update? Do you have an ID in the data to use, or a DOM node to use?
Allan
It does mean you have the cost of a loop, but that lookup must be done somehow :-). If you were going to be doing it a lot (.i.e. the fnUpdate is inside a loop itself) then you might want to create a mapping object, one where the parameters are the id's and the values are the array indexes - which would make it a simple property lookup.
Allan
NB. To anyone doing something similar, note the use of parseInt on 2nd argument to fnUpdate. Otherwise it doesn't work, presumably because datatables thinks a string is a reference to a DOM element rather than an index in aoData
[code]
var iCol = findColumnNumber('membershipStatusText', $('#programtable').dataTable().fnSettings().aoColumns);
var data = $('#programtable').dataTable().fnGetData();
for (var e in data) {
if (data[e].id == membership.programID) {
$('#programtable').dataTable().fnUpdate(membership.statusText, parseInt(e), iCol, false, false);
drawFilterRow();
break;
}
}
[/code]
Exactly that - it checks for a number, and if so it uses it as the index in the array.
Good to hear you got it working!
Allan