Can I invoke row

Can I invoke row

NoBullManNoBullMan Posts: 61Questions: 17Answers: 2

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

  • NoBullManNoBullMan Posts: 61Questions: 17Answers: 2

    Sorry, subject should have said "Can I invoke rowCallback:.

  • kthorngrenkthorngren Posts: 20,142Questions: 26Answers: 4,736

    You can call draw() which will cause rowCallback to run for the rows displayed on the page.

    Kevin

  • NoBullManNoBullMan Posts: 61Questions: 17Answers: 2

    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.

  • NoBullManNoBullMan Posts: 61Questions: 17Answers: 2

    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?

  • kthorngrenkthorngren Posts: 20,142Questions: 26Answers: 4,736

    Unless there is a way to remove all red text styling before rowCallback is called.

    I think what you did is correct.

    Is it possible to keep the text in "Search" when new data is fetched for table?

    How are you fetching the new data?

    If I update the database and need to re-fetch the data, can I continue applying the search text?

    If you are using ajax.reload() I think the search term will remain and be applied.

    Kevin

  • NoBullManNoBullMan Posts: 61Questions: 17Answers: 2

    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.

  • kthorngrenkthorngren Posts: 20,142Questions: 26Answers: 4,736
    edited January 2023

    Sounds like you are using jQuery ajax() instead of the ajax option? If yes it also sounds like you might be using destroy() or destroy to reinitialize the Datatable. If this is the case then use search() with no parameters to get the current search term before destroying. Then use search() to set the search term in initComplete.

    EDIT: Or, instead of destroying the Datatable you can use clear() followed by rows.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

  • kthorngrenkthorngren Posts: 20,142Questions: 26Answers: 4,736
    Answer ✓

    Another option might be to use ajax with ajax.data as a function to send the parameters to the server. See this example. You can then just use ajax.reload() to refresh the data and the search term will stay in place. Like this:
    http://live.datatables.net/jelehota/1/edit

    Kevin

  • NoBullManNoBullMan Posts: 61Questions: 17Answers: 2

    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.

Sign In or Register to comment.