Return html content via Ajax Laravel DataTable (using yajrabox package)
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
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);
}
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.
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');