Laravel-datatables: [7.0] Does not render HTML

Created on 27 Jan 2017  ·  18Comments  ·  Source: yajra/laravel-datatables

I'm using Laravel Datatables 7, but my table is not rendering HTML code. It was rendering HTML before, but when I updated to new Laravel DataTables to 7 from 6, it stopped rendering HTML in column. http://prntscr.com/e11n84

This is with Laravel DataTables 6 - http://prntscr.com/e11ph0

Most helpful comment

    return Datatables::of($this->users->getForDataTable($request->get('status'), $request->get('trashed')))
        ->editColumn('confirmed', function ($user) {
            return $user->confirmed_label;
        })
        ->addColumn('roles', function ($user) {
            return $user->roles->count() ?
                implode('<br/>', $user->roles->pluck('name')->toArray()) :
                trans('labels.general.none');
        })
        ->addColumn('actions', function ($user) {
           return $user->action_buttons;
        })
        ->rawColumns(['actions', 'confirmed'])
        ->withTrashed()
        ->make(true);

All 18 comments

Results are escaped by default on v7. Haven't documented all the breaking change yet but change log should provide some clues. Thanks!

How can I fix the same issue but in Service?

@brian2694 use the new method ->rawColumns(['column_with_html']).

I have similar problem, any example to use the rawColumns ?

    return Datatables::of($this->users->getForDataTable($request->get('status'), $request->get('trashed')))
        ->editColumn('confirmed', function ($user) {
            return $user->confirmed_label;
        })
        ->addColumn('roles', function ($user) {
            return $user->roles->count() ?
                implode('<br/>', $user->roles->pluck('name')->toArray()) :
                trans('labels.general.none');
        })
        ->addColumn('actions', function ($user) {
           return $user->action_buttons;
        })
        ->rawColumns(['actions', 'confirmed'])
        ->withTrashed()
        ->make(true);

Good!

Maybe allow

->rawColumns(['*'])

?

@divdax maybe try using ->escapeColumns([]) to achieve the same result.

thank u very much this is very helpful

->escapeColumns([]) worked for me. rawColumns('column_with_html') did not.
Using laravel 5.5 with "yajra/laravel-datatables-oracle": "~8.0"

Nara, rawColumns takes an array argument, so it should be rawColumns(['column_with_html'])

If wish to render a view in a column you could do this:

  ->editColumn('action', function(User $u){
       return view('users.action-btns', ['id'=>$u->id])->render();
  })

If I'm editing a column to render it with a view, then can it not be assumed by default that the view will be fully escaped by blade, and so does not need escaping again? A column edited with a view could be automatically set (by default) to be raw, just like an action column is already (though the docs don't mention that).

i have same problem, and my friend tell me to add ->escapeColumns([]), and its works.

`if ($request->ajax()) {
$ajax = Datatables::of(Admin::query())->escapeColumns([])->make(true);
return $ajax;
}

    $dataTable = $htmlBuilder
        ->addColumn(['data' => 'admin_id', 'name' => 'admin_id', 'class' => 'id-column', 'title' => 'Id', 'searchable' => false, 'render' => function () {
            $editUrl = route('backend.staff.edit', '');
            return "function(data,type,full,meta){ return '<a href=" . $editUrl . "/'+data+'>'+data+'</a>'; }";
        }])
        ->addColumn(['data' => 'username', 'name' => 'username', 'title' => trans('default.name'), 'class' => 'name-column', 'render' => function () {
            $editUrl = route('backend.staff.edit', '');
            return "function(data,type,full,meta){ return '<a href=" . $editUrl . "/'+full.admin_id+'>'+data+'</a>'; }";
        }])
        ->addColumn(['data' => 'ip_address', 'name' => 'ip_address', 'title' => trans('default.ip_address')])
        ->addColumn(['data' => 'last_login', 'name' => 'last_login', 'title' => trans('default.last_login'), 'class' => 'date-column', 'searchable' => false])
        ->addColumn(['data' => 'html_status', 'name' => 'html_status', 'title' => trans('default.status'), 'class' => 'status-column', 'searchable' => false])
        ->addAction(['data' => 'action_button', 'name' => 'action', 'title' => trans('default.action'), 'class' => 'actions-column'])`

@tonihidayt Do you understand what escapeColumns([]) does? It looks to me like it disabled ALL HTML escaping by setting NO columns to apply HTML escaping. This may be an easy way to fix one problem, that then leads you into a dangerous situation in other circumstances.


->escapeColumns([])

This is working correct for me.

I agree with @judgej . I do not want to risk escaping ALL columns, but the one that I want to make raw is an accessor and does not produce a raw result with $datatables->rawColumns(['full_link']);

@yajra Please update this documentation

https://datatables.yajrabox.com/eloquent/add-edit-remove-column

Was this page helpful?
0 / 5 - 0 ratings