class

class

k-georgek-george Posts: 13Questions: 3Answers: 0
edited November 22 in Free community support

https://live.datatables.net/vohiyelo/3/edit

Error as seen in the test case:

Trying to add two classes to all odd rows.

How to add two classes to all odd rows that needs to work with both client side and server side process?

Answers

  • allanallan Posts: 65,352Questions: 1Answers: 10,842 Site admin
     drawCallback: function () { $('#example tbody tr').removeClass('odd'); $('#example tbody tr:odd').addClass('odd') } 

    That will remove the odd class from any existing rows in the body (for cases when a row was odd, but has been moved to become even!), and then adds it on.

    However, if it is just for styling, then just use CSS:

    #example tbody tr:nth-child(odd) { ... } 

    Allan

  • k-georgek-george Posts: 13Questions: 3Answers: 0

    Thanks for the reply.

    Two more matters to get clarity.

    1- Difference between (in simple words)

    a) "rowCallback": function ()

    b) "drawCallback": function ()

    2 How to add it rowCallback / drawCallback function on a button click ?

    george.

  • kthorngrenkthorngren Posts: 22,333Questions: 26Answers: 5,135

    The option rowCallback and drawCallback docs explain it best.

    a) "rowCallback": function ()

    This callback allows you to 'post process' each row after it have been generated for each table draw, but before it is rendered into the document.

    b) "drawCallback": function ()

    Function that is called every time DataTables performs a draw.

    You can use a console.log() statement in each callback to see the behavior and when the run.

    How to add it rowCallback / drawCallback function on a button click ?

    Call draw() to execute the option rowCallback and drawCallback functions.

    Kevin

  • k-georgek-george Posts: 13Questions: 3Answers: 0

    Thanks for the reply.

    Actually don't know the logic required to add the below with a button click

    "drawCallback": function () {
    $('#example tbody tr').removeClass('odd');
    $('#example tbody tr:odd').addClass('odd')
    }

    can you show me onetime ?

    george

  • kthorngrenkthorngren Posts: 22,333Questions: 26Answers: 5,135

    Like this:
    https://live.datatables.net/vohiyelo/7/edit

    Not sure having a button for just redrawing the table to execute that particular code in drawCallback is very useful. drawCallback will run anytime the table is drawn for example anytime the table is sorted, searched or changing the page.

    What are you trying to do with the button?

    Kevin

  • k-georgek-george Posts: 13Questions: 3Answers: 0

    What are you trying to do with the button?

    Answer - only call or function the 'drawCallback' on that button click.

    I don't know that 'how to do that.

    george

  • kthorngrenkthorngren Posts: 22,333Questions: 26Answers: 5,135
    edited November 22

    As I said to execute the drawCallback function call the draw() API. This is what my example shows:
    https://live.datatables.net/vohiyelo/7/edit

    However maybe you just want to execute an independent function when clicking a button. Something like this:
    https://live.datatables.net/vohiyelo/9/edit

     $('#update-class').on('click', function () { $('#example tbody tr').removeClass('odd'); $('#example tbody tr:odd').addClass('my-odd') }); 

    Sorry, I'm still not clear on what exactly your requirement is.

    Kevin

Sign In or Register to comment.