How would I manipulate data in an existing table?
How would I manipulate data in an existing table?
I have a DataTable already loaded in my page which contains columns of integers.
I want to offer alternate views where the integers are divided by a number to create an average. Column 1 may be divided by x and column 2 by y, so different divisions. This could be toggled by clicking on a button.
I do not want to re-load the table due to performance issues but change the values in the cells only.
I've read the API may be able to be used here but I do not fully understand it yet.
I know how to create the button, just not the DataTables part.
Can anybody explain how this would be done please (i assume in JavaScript)?
Thanks!
This question has an accepted answers - jump to answer
Answers
I would consider using
columns.render
. Here is an example:http://live.datatables.net/kizisema/1/edit
rows().invalidate()
is used to invalidate all the data so that Datatables runs thecolumns.render
function when usingdraw()
. Otherwise, for efficiency, the render function won't run since there are no changes. Also thedraw()
uses the parameterfalse
which stays on the same page. Using this method allows for searching and sorting by the calculated value.Kevin
Hey Kevin - thanks for the informative response!
I've been trying it out but cannot seem to apply it into my code. My table is loading as expected and my "createdRow" feature is working correctly, but sadly not the columnDefs/divide feature.
One noticeable difference is you defined your data within the code whereas mine is parsed from a db into html then referenced via the class. Should this make a difference?
Any help appreciated and thanks for your time so far! code below.
HTML snippet of the checkbox
JavaScript
Looks like you need to pass the
data
parameter forrows().invalidate()
. Like this:http://live.datatables.net/kizisema/3/edit
Kevin
That did the trick! Thanks so much, Kevin. Great info.