How can I add class to the row on table load?

How can I add class to the row on table load?

scrzscrz Posts: 1Questions: 1Answers: 0

I have table #dt-transaction
I want to highlight row of 'new' transactions

var dt_transaction = $('#dt-transaction').DataTable({
        "processing": true,
        "ajax": {

            "url": "/cabinet/get/1",
            "type": "POST",
            "dataSrc": function (json) {
                // console.log(json)
                return json;
            }
        },
        columns: [{
            "data": "date_pub"
        },
        {
            'data': 'client'
        },
        {
            'data': 'sum'
        }
        ],
        "language": lang[0]
    });

// get data['new'] from row on table body load
$('#dt-transaction tbody').load('tr', function () {
        var data = dt_transaction.row(this).data();
        console.log(this) //here console shows that row is undefined
        if (data['new'] == 1) {
            $(this).addClass('table-success')
        }
    });

Answers

  • allanallan Posts: 63,214Questions: 1Answers: 10,415 Site admin

    I'd suggest using rowCallback for this. That will remove any issues with the async behaviour of ajax.

    Allan

This discussion has been closed.