0

How Can I add a class in this render where I ask if the office is enable or disable, if this disable should add this class table-active. I was searching for some similar question but none worked.

var table = $('#tbl_1').DataTable({
            "order": [
                [1, "asc"]
            ],
            "destroy": true,
            "ajax": {
                "method": "POST",
                "url": "JSON/Office.php"
            },
            "iDisplayLength": 15,
            "columns": [ {
                "data": "Office",
                "width": "20%"
            },  {
                "data": "Status",
                "searchable": false,
                "sortable": false,
                "aling": "center",
                "render": function(data, type, row) {
                    var Status = row["Status"];
                    if (Status == 'FALSE') {
                        return '<button class="btn btn-sm btn-success active" onclick="enable_item(this)"title="Active">Active</button>';
                    } else {
                        return '<button class="btn btn-sm btn-danger disable" onclick="disable_item(this)" title="Disable"> Disable</button>';
                    }
                }
            }],
            "dom": '<"dt-buttons"Bf><"clear">lirtp',
            "paging": true,
            "autoWidth": true,
            buttons: [{
                extend: 'excel',
                text: 'Excel'
            }]
        });

One of the answer I found was this $(row).addClass("table-active"); but still not working :(. I hope I explain well greetings

4
  • Do you want to assign some class to entire row or some particular cell? Commented Mar 5, 2019 at 15:50
  • @ygorbunkov the row Commented Mar 5, 2019 at 15:51
  • Than, I believe, provided answer is supposed to fulfill your requirement? Doesn't it? Commented Mar 5, 2019 at 15:54
  • Doesnt work cause Im using render Commented Mar 5, 2019 at 19:08

1 Answer 1

5

If I understood you correct and you want to add a class to the <tr> element you can use the createdRow hook - https://datatables.net/reference/option/createdRow.

$('#tbl_1').dataTable({
  "createdRow": function( row, data, dataIndex ) {
    if ( data["Status"] == false ) {
      $(row).addClass( 'table-active' );
    }
  }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.