Toggle a class name to a column in DataTable

Toggle a class name to a column in DataTable

Francesco Di SciascioFrancesco Di Sciascio Posts: 8Questions: 3Answers: 0

Hi everyone,
I have a datatable initialized with a className in this way:

columnDefs: [{className: "editable", targets: [COLUMN_NUMBER]}]

Is there a way to add / remove the same className when something changes in my view?
I need to have the column 'editable' only in certain cases, and not editable in others.

Thanks for your help

This question has an accepted answers - jump to answer

Answers

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

    Try cells().nodes(). You will probably want a row-selector of null and a cell-selector of the column. Something like this:

    var table = $('#example').DataTable();
     
    var cells = table
        .cells( null, COLUMN_NUMBER )
        .nodes();
     
    $( cells ).toggleClass( 'editable' );
    

    Kevin

  • Francesco Di SciascioFrancesco Di Sciascio Posts: 8Questions: 3Answers: 0

    Kevin,
    your solution seems not working correctly.
    No class is added to cells of the COLUMN_NUMBER column

  • Francesco Di SciascioFrancesco Di Sciascio Posts: 8Questions: 3Answers: 0

    Kevin,
    Maybe a .to$() is missing, but the fact is that, when an event occurs, I do not reinitialize the table, but I only do the following:

    totaliFatturaTable.clear().draw();
    

    I need something that allows me to toggle the editability of the cells of COLUMN_NUMBER, because with a toggleClass or an addClass after the clear nothing changes

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

    That code snippet works here:
    http://live.datatables.net/joqagaxu/1/edit

    Please provide a link to your page or a test case replicating the issue so we can help debug.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • Francesco Di SciascioFrancesco Di Sciascio Posts: 8Questions: 3Answers: 0

    You are right, your sample works.
    It's hard to put out a snippet for my project because of the complexity, but I suppose it could depend on how I retrieve the Datatable.
    To solve it I've used the following:

    totaliFatturaTable.clear().destroy();
    

    and then I have called a new initialization of the table. I need to clear the table because it owns total given by other table selections (like an invoice).
    Thanks for your help!

Sign In or Register to comment.