I am using this package https://datatables.yajrabox.com/starter 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:
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.
Okay, the issue appears to be a undocumented breaking change in the new 7.x version of the library: https://github.com/yajra/laravel-datatables/issues/949
In my case, I've fixed it 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>';
})
->rawColumns(['is_admin'])
->make(true);
}
Just put your column in below method
escapeColumns()
Example
return $dataTable->addIndexColumn()
->editColumn('active', function(SettingDropdownValue $model) {
if($model->active === true){
return '<span class="text-center"><img src="'.\URL::asset('/images/active.png').'" border="0" width="15" /></span>';
} else {
return '<img src="'.\URL::asset('/images/inactive.png').'" border="0" width="15" />';
}
})
->escapeColumns('active')
->addColumn('action', 'setting_dropdown_values.datatables_actions');
I'm working on a project right now and face the same problem and fix it using ->escapeColumns([])
$users = DB::table('users')->select('*');
return datatables()->of($users)
->removeColumn('password')
->editColumn('created_at', function ($user) {
return date('d-m-Y', strtotime($user->created_at));
})
->editColumn('role_id', function ($user) {
return Role::findOrFail( $user->role_id)->name;
})
->editColumn('is_active', function ($user) {
return ($user->is_active == 1) ? 'active' : 'suspended';
})
->addColumn('actions', function ($user) {
return '<a href="#edit-'.$user->id.'" class="btn btn-xs btn-primary"><i class="glyphicon glyphicon-edit"></i> Edit</a>';
})
->escapeColumns([])
->make(true);
Source: yajra/laravel-datatables
Add another column labeled rawcolumn
with the name you assign to the individual buttons as indicated below to fix the issue:
Route::get('user-data', function() {
$model = App\User::query();
return DataTables::eloquent($model)
->addColumn('link', '<a href="#">Html Column</a>')
->addColumn('action', 'path.to.view')
->rawColumns(['link', 'action'])
->toJson();
});
This rawColumns(['link', 'action'])
is what does the magic. Your last statement might end with ->make(true)
, the rawColumn
is what does the magic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With