将数据表的行关联到 bootstrap 开关(使用另一个列字段)

Associate row of datatable to bootstrap switch (using another column field)

我正在使用数据tables 和bootstrap 切换插件来启用和禁用用户。当我点击开关时,我通过 ajax 调用一个网络服务,我的其余网络服务将用户状态切换到数据库中。问题是知道从数据 table 行中选择了哪个用户并将其传递到我的 Web 服务。这是 table 初始化:

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();
    }

这是 bootstrap 切换事件:

$('#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";
            }
        });
    });

我想在构建过程中将用户名设置为 bootstrap 开关的字段,但是怎么做呢?我应该这样做:

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

但它不起作用,因为设置的是用户名而不是 ajax 调用返回的值。你能帮帮我吗?

我用这段代码解决了(我希望它对某人有用)

{ 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')

得到值