How Do I Remove Classes From a Using Render?

How Do I Remove Classes From a Using Render?

puffsterpuffster Posts: 61Questions: 22Answers: 0

I think I'm close but I'm not doing something quite right, I have a parent table that you can drill down into. The final row is an "Overall" row and there's nothing to drill into, so I want to remove the classes that allow a user to select/drill into that row. I've tried everything I can think of but cannot get the classes to remove. Below is a snippet of what my code looks like, can somebody show me the error of my ways please!?

            columns: [
                {
                    className: 'cumulative-details-control openClose',
                    width: "5%",
                    data: null,
                    defaultContent: '',
                    render: function (data, type, full, meta) {
                        if (data.subject == "Overall") {
                            $(this).removeClass('cumulative-details-control');
                            $(this).removeClass('openClose');
                        }
                    }
                },

This question has an accepted answers - jump to answer

Answers

  • Rob BrunRob Brun Posts: 56Questions: 2Answers: 14
    edited June 2017 Answer ✓

    Hi puffster, maybe in your createdRow callback do something like this

    "createdRow": function( row, data, dataIndex ) {
         if(data.subject === "overall"){
            $(row).removeClass('cumulative-details-control');
            $(row).removeClass('openClose');
          }
    }
    

    https://datatables.net/reference/option/createdRow

    Shay

  • allanallan Posts: 61,970Questions: 1Answers: 10,160 Site admin

    Yes, you can't access the node in the columns.render function at the moment because it might not have been created when that function is called. You have to use a callback as Shay suggests.

    Allan

  • puffsterpuffster Posts: 61Questions: 22Answers: 0

    Thank you both, that was the issue!! I had to make a slight tweak to the jQuery to remove the class, but it's humming now!!

                createdRow: function (row, data, dataIndex) {
                    if (data.subject == "Overall") {
                        $(row).find('td').removeClass('cumulative-details-control');
                        $(row).find('td').removeClass('openClose');
                    }
                }
    
This discussion has been closed.