className or class

className or class

lyndonwillyndonwil Posts: 40Questions: 5Answers: 0

In my project, when using columnDefs, i have been using class as opposed to className (https://datatables.net/reference/option/columns.className)

Is there any reason i should use className rather than class ?

{ "class": "searchDropdown center", "searchable": true, "targets": [5]},

or

{ "className": "searchDropdown center", "searchable": true, "targets": [5]},

Using className does cause an issue when running the following code in INITCOMPLETE, as it can't find the class

 (var colClassname = currentColumn.class;)
"initComplete": function(settings, json) {
            
            if (fSearchRows==true){
             
                $.each( settings.aoColumns, function( indexColumn, currentColumn ) {
                    columnTh = $( ".searchRow th:nth-child(" + (indexColumn+1)+ ")");
                    
                    var colClassname = currentColumn.class;
                    
                    if (typeof colClassname !== "undefined")
                    {
                    
                        if (colClassname.indexOf("searchDropdown") >=0){
                            
                            // Loop Drop Down Search Information
                            for(var key in fdropDownSearchCols) {
                                 if(indexColumn==key){
                                     var optionHtml= createOption(fdropDownSearchCols[key], indexColumn, columnTh);
                                 }
                            }
                        }
                    }
                
                });
                
                table.draw();
            }   
        },



This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,765Questions: 1Answers: 10,111 Site admin
    Answer ✓

    Is there any reason i should use className rather than class ?

    Yes, class is a reserved word in Javascript. Some (not all!) Javascript engines will throw an error if they see something like:

    { class: "..." }
    

    They would be perfectly fine if it were quoted JSON style, but not the above (Safari in particular I'm thinking of - although it has been a little while since I've used Safari, so it might have changed).

    That class is available as the class name setting in DataTables is purely due to the automated Hungarian notation compatibility - i.e. sClass becomes class.

    I would recommend className for compatibility and also consistency (it is what I will be using in all other extensions here on in).

    Allan

  • lyndonwillyndonwil Posts: 40Questions: 5Answers: 0

    That makes sense

    I had some old code in the function which still referred to class as opposed to className
    var colClassname = currentColumn.className;

    working perfectly now with className

    Thanks

This discussion has been closed.