Anirudh
Anirudh

Reputation: 3408

How to modify Datables data while binding to the view in Laravel?

Here is my existing JS code:

    var CategoriesTablewithFilter = function(){
    var table = $('#catDatatable');
    var url = $('#url').val();
    var tableObj = table.DataTable( {
        "serverSide": true,
        "responsive": true,


        "aoColumnDefs": [
                          { "bSearchable": true, "aTargets": [ 1 ] },
                        ],

        ajax:
         {
            url: url, 
            dataSrc: 'data',
        },

        columns: [ 
            { data: 'id'},
            { data: 'name'},
            { data: 'status'},

        ],

    } );

}

Status is a Boolean field which returns 1 or 0. Is there anyway I can change 1, 0 to Strings - Active/InActive

Upvotes: 0

Views: 1102

Answers (2)

Rajender Joshi
Rajender Joshi

Reputation: 4205

Try debugging you ajax response using console.log(data) output to fetch status's value and use render function to display render the final output.

columns: [
    { data: 'id'},
    { data: 'name'},
    {
        data: 'status',
        render: function (data, type, full, meta) {
            return full.status == true ? 'Active' : 'Inactive';
        }
    }
]

Or you could use

columns: [
    { data: 'id'},
    { data: 'name'},
    {
        data: 'status',
        render: function (data, type, full, meta) {
            return data === '1' ? 'Active' : 'Inactive';
        }
    }
]

Upvotes: 0

rad11
rad11

Reputation: 1561

You should use render function for column:

var CategoriesTablewithFilter = function () {
    var table = $('#catDatatable');
    var url = $('#url').val();
    var tableObj = table.DataTable({
        "serverSide": true,
        "responsive": true,
        "aoColumnDefs": [
            {"bSearchable": true, "aTargets": [1]},
        ],
        ajax:
                {
                    url: url,
                    dataSrc: 'data',
                },
        columns: [
            {data: 'id'},
            {data: 'name'},
            {data: 'status', render: function (data, type, row, meta) {
                return data == 1 ? 'Active' : 'InActive';
            }}
        ],
    });
}

Upvotes: 1

Related Questions