2016-02-08 53 views
0

我正在使用datatables和bootstrap開關插件來啓用和禁用用戶。當我點擊開關時,我通過ajax調用Web服務,而我的其他Web服務將用戶的狀態切換到數據庫。問題是知道從數據錶行中選擇用戶並將其傳遞給我的Web服務。這是表initialitanion:datatable到自舉開關的關聯行(使用另一個列字段)

if (! $.fn.DataTable.isDataTable('#usersTable')) { 
     userTable = $('#usersTable').DataTable({ 
      responsive: true, 
      //disable order and search on column 
      columnDefs: [ 
         { 
          targets: 1, 
          orderable: false, 
          searchable: false, 
         } 
         ], 
      //fix problem with 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 for 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(); 
    } 

這是自舉切換事件:

$('#usersTable').on('switchChange.bootstrapSwitch', 'input[name="my-checkbox"]', function(event, state) { 
     //CSRF attribute for 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); 
      }, 
//   all right with rest call 
      success : function(data) { 
//    No exception occurred 
       if (data.status==true){ 
//     Also the field are right(for e.g. form value) 
        if(data.success==true){ 


        } 
        else{ 
//      code if there are some error into form for example 
        } 
       } else { 
//     code exception 
        notifyMessage(data.result, 'error'); 
       } 
      }, 
//   error during rest call 
      error : function(data) { 
       window.location.href = "/ATS/500"; 
      } 
     }); 
    }); 

我想它的建設,但如何在設置用戶名的領域引導開關?我應該做這樣的事情:

if (data) { 
    return '<input value=username type="checkbox" name="my-checkbox" checked>'; 

,但它不工作,因爲設置的用戶名,而不是價值從AJAX調用返回。你可以幫我嗎?

回答

1

我解決了這個代碼(我希望它是有用的人)

{ 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">'; 
    } 
} 

,然後我得到$(this).attr('data')

相關問題