Can I invoke row
Can I invoke row
Link to test case:
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem:
I am applying some custom CSS to rows that meet a specific condition, namely if Last Modified timestamp is beyond a user-set value (default 8 hours), using rowCallback. Works fine.
When user changes this threshold value, I have to fetch the entire ajax data and repopulate which is not efficient and I was wondering if there is a way of manually invoking rowCallback so as to only apply styling based on new threshold value.
rowCallback: function (row, data, index) {
// Use red text for the row if last modified date is more than 8 hours ago
var threshold = 0 - $("#tbOpenThreshold").val();
var now = moment().subtract(tzCorrection, 'hours');
var timeDiff = moment.duration(moment(data.LASTMODIFIEDON).diff(now));
var hours = timeDiff.asHours();
if (hours < threshold)
$('td', row).css('color', 'Red');
},
This question has an accepted answers - jump to answer
Answers
Sorry, subject should have said "Can I invoke rowCallback:.
You can call
draw()
which will causerowCallback
to run for the rows displayed on the page.Kevin
Thank you.
Looks like I had to add an "else" statement to rowCallback to redraw the rows in black (remove red text styling) if new condition was not satisfied. Otherwise it would keep the previously red text as red. Unless there is a way to remove all red text styling before rowCallback is called.
Is it possible to keep the text in "Search" when new data is fetched for table?
Foe example, If I update the database and need to re-fetch the data, can I continue applying the search text?
I think what you did is correct.
How are you fetching the new data?
If you are using
ajax.reload()
I think the search term will remain and be applied.Kevin
Thank you Kevin.
I make an Ajax call to a web service to get json data and use it to populate the table.
For instance, I might change a checkbox list's selected checkboxes, which is used as a parameter to Ajax call and repopulate the table. But want to be able to keep the search string so I don't have to type it in again.
Sounds like you are using jQuery ajax() instead of the
ajax
option? If yes it also sounds like you might be usingdestroy()
ordestroy
to reinitialize the Datatable. If this is the case then usesearch()
with no parameters to get the current search term before destroying. Then usesearch()
to set the search term ininitComplete
.EDIT: Or, instead of destroying the Datatable you can use
clear()
followed byrows.add()
to update the data without the need to keep track of the search term.If my assumptions are wrong then please post the code you are using to refresh the Datatable.
Kevin
Another option might be to use
ajax
withajax.data
as a function to send the parameters to the server. See this example. You can then just useajax.reload()
to refresh the data and the search term will stay in place. Like this:http://live.datatables.net/jelehota/1/edit
Kevin
Thanks a lot Kevin.
Your comments made me take a closer look at how I populate the table and I noticed in addition to .clear() before .rows.add(), I also called .search('').draw(). Once I commented out this line, it worked fine and kept the search string when table was repopulated.
Basically, operator error!
Thank you very much for your prompt responses.