luca February 2016

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

I'm using datatables and bootstrap switch plugins to enable and disable users. When I click on switch I call a web service throught ajax and my rest web services switch status of user into database. The problem is know whitch user is selected from datatables row and pass it to my web service. This is table initialitanion:

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>'
                        }
                        ],
                        "f        

Answers


luca February 2016

I solved with this code (I hope it is useful for 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 then I get the value with $(this).attr('data')

Post Status

Asked in February 2016
Viewed 1,239 times
Voted 9
Answered 1 times

Search




Leave an answer