rowCallback not being called on update

rowCallback not being called on update

zaaynzaayn Posts: 4Questions: 1Answers: 0

Hi, I'm trying to update a particular column using row.every() and apply custom styles, but it seems rowCallback is not called on update. Is it because rows are not being drawn and only data is updated? In such case, how should I update the data/column or apply custom styles? Moreover, I'm not really fan of using rowCallback due to its inefficiency (on large datasets), but I couldn't find any other ways to achieve the behavior I want. How do I proceed on this?

This question has an accepted answers - jump to answer

Answers

  • rf1234rf1234 Posts: 2,806Questions: 85Answers: 406
    edited July 2022

    If it is every row in a particular column that you would like to update you don't really need "rowCallback" in my opinion.

    "columnDefs" could really help you. Like in this example from my own coding:

    columnDefs: [
       // targets may be classes
        {   targets: "hiddenCols", visible: false },
        {   targets: "noSelectCols", className: "noSelect"},
        {   targets: "cnt", className: "text-center"},
    ],
    select: {
        style:    'single',
        selector: 'td:not(.noSelect)' 
    },
    

    The first one is obvious. I assign class "hiddenCols" to <th> elements in my HTML and they are hidden right away. That makes it dynamic when changes are made.

    The second and third ones are more interesting: Based on the class of the <th> element in your HTML you can assign a class to every cell (<td>) of the respective column.

    With "text-center" I apply a bootstrap class to every cell of the column. Just replace this with a class that has your custom styles and you should be done. I need "noSelect" to make sure the selector for column visibility is dynamic, too.

    One more thing: Using row.every() inside rowCallback mostly isn't very effective: rowCallback is called for every row anyway. Looping through all of the rows for every row is a lot of looping ...

  • zaaynzaayn Posts: 4Questions: 1Answers: 0
    edited July 2022

    Hey rf1234, thanks for taking your time answering the question.

    Yes, I've already tried using columnDefs but the problem is I need to apply classes/styles based on certain conditions. So that didn't help. Unless it's possible to apply classes through render that I'm not aware of?

    Also, I'm not calling row.every() inside rowCallback. It's just that after the update using row.every(), 'rowCallback' isn't fired. Thanks for your concern tho :)

  • allanallan Posts: 61,667Questions: 1Answers: 10,096 Site admin
    Answer ✓

    rowCallback will be called for every row, when the table is drawn. Updating a row doesn’t automatically redraw the table - you need to call draw().

    Perhaps you can show us your code?

    Allan

  • zaaynzaayn Posts: 4Questions: 1Answers: 0
    edited July 2022

    Hi @allan draw() did the trick. Thanks!

    On a second thought, is it possible to fire createdRow using draw() or any other method (w/o completely resetting the table)? Or, apply conditional classes/styles via columnDefs.render()?

  • zaaynzaayn Posts: 4Questions: 1Answers: 0
    edited July 2022

    On a second thought, is it possible to fire createdRow using
    draw() or any other method (w/o completely resetting the table)? Or,
    apply conditional classes/styles via columnDefs.render()?

    nevermind. figured.

  • kthorngrenkthorngren Posts: 20,276Questions: 26Answers: 4,765
    edited July 2022

    On a second thought, is it possible to fire createdRow using draw()

    createdRow is called only once when the row is created. Its not called again. rowCallback is basically the same except it runs each time the table is drawn.

    Or, apply conditional classes/styles via columnDefs.render()?

    columns.render is not meant for applying styles to dom elements as the element might not by in the dom when columns.render runs.

    Just curious why you are asking about alternatives to rowCallback. If you are updating the data that might affect the row styling then rowCallback is the callback to use.

    Kevin

Sign In or Register to comment.