How can I use different label color for datatable fields?

How can I use different label color for datatable fields?

lucacali87lucacali87 Posts: 30Questions: 10Answers: 2

Hi, I'm using datatable with ajax call to fill values of several users(each user has username, enabled, role ....)
I would like to improve its appearance, so I thought to use different label depending on user Role and show toggle for enabled field.
So if user is ADMIN I'd like to use red label, if he is USER white label etc... for the switch it can be on or off. I can make this if I use static datatable, but I prefer ajax call to be able to refresh its.
This is my ajax call :

if ( ! $.fn.DataTable.isDataTable( '#usersTable' ) ) {
        userTable = $('#usersTable').DataTable({
            responsive: true,
            select: {
                style: 'single'
            },
            "ajax": "table",
            "columns": [
                        { "data": "username" },
                        { "data": "enabled" },
                        { "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>'
                        }
                        ]
        });
    }
    else {
        userTable.ajax.url("table").load();
    }

How can I make these features?Thanks

This question has an accepted answers - jump to answer

Answers

  • lucacali87lucacali87 Posts: 30Questions: 10Answers: 2

    render: function ( data, type, row ) this is the right function :)

  • glendersonglenderson Posts: 231Questions: 11Answers: 29
    Answer ✓

    I've always found that render() is good for changing content inside the cell. So if you want to use a label inside the cell, it can work.

    I'd rather apply a class to the td element instead. For that rowCallback or createdrow seem to handle it better.
    http://datatables.net/release-datatables/examples/advanced_init/row_callback.html

    But, if you really want to get fancy, you start passing additional data that contains your td class or style, and then use rowCallback to read the class or style and apply it to a the td containing the data you want to affect.

This discussion has been closed.