ErcanE
ErcanE

Reputation: 1641

How To Display Bootstrap Toggle Switch with DataTables via Render

I am using Bootstrap Toggle, CSS, and JavaScript loads fine please see image.However when I use inside datatable (Done coloumn) custom JavaScript and styling doesn't work.I am getting data via Ajax

Datatable Script

$('#sampleTable').DataTable( {
      "ajax": {
        "url": "/generalTodo",
        "dataSrc": ""
      },    
"columns": [
            {
             "sortable": true,
             className: 'centerize',
             render: function ( data, type, full, meta ) {

                return '<input type="checkbox" checked data-toggle="toggle" data-on="Ready" data-off="Not Ready" data-onstyle="success" data-offstyle="danger">';


             }
            },.....

enter image description here

Upvotes: 7

Views: 15844

Answers (2)

Goran Siriev
Goran Siriev

Reputation: 703

if you going to use loop add class

class="my_switch"

return '<input id="toggle-demo" class="my_switch" type="checkbox" checked data-toggle="toggle" data-on="Ready" data-off="Not Ready" data-onstyle="success" data-offstyle="danger">';

End on fnDrawCallback

"fnDrawCallback": function() {
   $('.my_switch').bootstrapToggle();
},

Upvotes: 1

ErcanE
ErcanE

Reputation: 1641

I have found the solution:

return '<input id="toggle-demo" type="checkbox" checked data-toggle="toggle" data-on="Ready" data-off="Not Ready" data-onstyle="success" data-offstyle="danger">';

and

"fnDrawCallback": function() {
            $('#toggle-demo').bootstrapToggle();
        },

Loading Js after table finish the loading!

Upvotes: 11

Related Questions