vba_user
vba_user

Reputation: 125

Jquery Datatable - Pulling data from server - how to set class for columns

I have a simple code to pull data using ajax in my datatable:

$('#datatable').dataTable( {

    "pageLength": 50,
    "ajax": "/test/pull/",

     "columns": [
        { "data": "a" },
        { "data": "b" },
        { "data": "c" },
        { "data": "d" },
        { "data": "r" },
        { "data": "f" }
    ],

    "columnDefs": [ {
            "render": function ( data, type, row ) {
                return '<button type="button" class="btn btn-success btn-sm">Click</button>'
            }, "targets": 2
        }
    ]
} );

I am wondering how to set a class attribute for columns 3, 4 and 5

Is there a chance to do this using columns rendering?

Will be thankful for your help,

Upvotes: 1

Views: 78

Answers (1)

yash
yash

Reputation: 2271

hope this work for you.

Using className to assign a class to the cells in the first column with columnDefs:

$('#datatable').dataTable( {
  "columnDefs": [
    { className: "my_class", "targets": [ 0 ] }
  ]
} );


Using className to assign a class to the cells in the first column with columns:

$('#example').dataTable( {
  "columns": [
    { "data": "a" , className: "my_class" },
    { "data": "b" },
    { "data": "c" },
    { "data": "d" },
    { "data": "r" },
    { "data": "f" }
  ]
} );

If you want to add more than one class, just separate them by spaces, as you would in a normal class html attribute:

{ className: 'my_class other_class' }

Upvotes: 1

Related Questions