vendredi 3 février 2017

Return html content via Ajax Laravel DataTable (using yajrabox package)

I am using this package http://ift.tt/2jM1ifW to implement ajax datatable in my laravel application.

In my controller class, I have the following method to return the data for the datatables like this:

function ajaxList()
{
    // Load users with users
    $users = User::with('group', 'organisation');

    // Finished
    return Datatables::eloquent($users)
        ->editColumn('is_admin', function(User $user) {
            return '<i class="fa fa-'. ($user->is_admin ? 'check' : 'times') .'" aria-hidden="true"></i>';
        })
        ->make(true);
}

On the view, I render the table and initiate the ajax request like this:

<table id="users-table" class="table table-hover table-bordered" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th>User ID</th>
            <th>Is Admin?</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Created At</th>
            <th>Updated At</th>
            <th>Action</th>
        </tr>
    </thead>
</table>
<script>
    $('#users-table').DataTable({
        processing: true,
        serverSide: true,
        ajax: '/users/ajaxList',
        columns: [
            {data: 'id', searchable: false },
            {data: 'is_admin', searchable: false },
            {data: 'first_name'},
            {data: 'last_name'},
            {data: 'created_at', searchable: false },
            {data: 'updated_at', searchable: false },
            {data: 'action', searchable: false, orderable: false }
        ]
    });
</script>

When this renders, the "is_admin" column appears to show the raw html rather than rendering the font awesome icon like this:

enter image description here

Any ideas how to fix this? I tried returning the column data like this also:

return '{!! <i class="fa fa-'. ($user->is_admin ? 'check' : 'times') .'" aria-hidden="true"></i> !!}';

This didn't help either.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2jM62SX
via IFTTT

Aucun commentaire:

Enregistrer un commentaire