javascript - Associate row of datatable to bootstrap switch (using another column field) -
i'm using datatables , bootstrap switch plugins enable , disable users. when click on switch call web service throught ajax , rest web services switch status of user database. problem know whitch user selected datatables row , pass web service. table initialitanion:
if ( ! $.fn.datatable.isdatatable( '#userstable' ) ) { usertable = $('#userstable').datatable({ responsive: true, //disable order , search on column columndefs: [ { targets: 1, orderable: false, searchable: false, } ], //fix problem responsive table "autowidth": false, "ajax": "table", "columns": [ { "data": "username" }, { data: "enabled", render: function ( data, type, row ) { if (data) { return '<input data=data=row.username type="checkbox" name="my-checkbox" checked>'; } else { return '<input data=data=row.username type="checkbox" name="my-checkbox">'; } } }, { "data": "role.role"}, { "data": "clientversion.name" }, { data: null, classname: "center", defaultcontent: '<button type="button" class="btn btn-danger" id="deletelicense" data-toggle="modal" th:attr="data-href=${license.idclientlicense}" data-target="#deletelicensemodal">delete</button>' } ], "fndrawcallback": function() { //initialize checkbos enable/disable user $("[name='my-checkbox']").bootstrapswitch({size: "small", oncolor:"success", offcolor:"danger"}); //$('#togglechecked').bootstrapswitch({size: "small", oncolor:"success", offcolor:"danger"}); } }); } else { usertable.ajax.url("table").load(); }
and bootstrap switch event :
$('#userstable').on('switchchange.bootstrapswitch', 'input[name="my-checkbox"]', function(event, state) { //csrf attribute spring security var token = $("meta[name='_csrf']").attr("content"); var header = $("meta[name='_csrf_header']").attr("content"); $.ajax({ type : "post", url : "status/" + //username setted above, beforesend:function(xhr) { xhr.setrequestheader(header, token); }, // right rest call success : function(data) { // no exception occurred if (data.status==true){ // field right(for e.g. form value) if(data.success==true){ } else{ // code if there error form example } } else { // code exception notifymessage(data.result, 'error'); } }, // error during rest call error : function(data) { window.location.href = "/ats/500"; } }); });
i thought set username field bootstrap switch during construction how? should this:
if (data) { return '<input value=username type="checkbox" name="my-checkbox" checked>';
but doesn't work because set username , not value returned ajax call. can me?
i solved code (i hope useful someone)
{ data: "enabled", render: function ( data, type, row ) { if (data) { return '<input data="'+row.username+'" type="checkbox" name="my-checkbox" checked>'; }else { return '<input data="'+row.username+'" type="checkbox" name="my-checkbox">'; } }
and value $(this).attr('data')
Comments
Post a Comment