MrEnergy
MrEnergy

Reputation: 77

Laravel DataTables does not render HTML

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

$(function() {
    $('#users-table').DataTable({
        processing: true,
        serverSide: true,
        ajax: {
            url: '{{ route("admin.access.user.get") }}',
            type: 'post',
            data: {status: 1, trashed: false}
        },
        columns: [
            {data: 'id', name: '{{config('access.users_table')}}.id'},
            {data: 'name', name: '{{config('access.users_table')}}.name', render: $.fn.dataTable.render.text()},
            {data: 'email', name: '{{config('access.users_table')}}.email', render: $.fn.dataTable.render.text()},
            {data: 'confirmed', name: '{{config('access.users_table')}}.confirmed'},
            {data: 'roles', name: '{{config('access.roles_table')}}.name', sortable: false},
            {data: 'created_at', name: '{{config('access.users_table')}}.created_at'},
            {data: 'updated_at', name: '{{config('access.users_table')}}.updated_at'},
            {data: 'actions', name: 'actions', searchable: false, sortable: false}
        ],
        order: [[0, "asc"]],
        searchDelay: 500
    });
});

Upvotes: 2

Views: 3451

Answers (3)

Shoaib Khan
Shoaib Khan

Reputation: 1060

Add the column with html as shown on other answers and after all add this ->rawColumns(['html_column', 'another_html_column']) before ->toJson() to render all the html column as sent by the server

Upvotes: 1

Marvin Nario Machitar
Marvin Nario Machitar

Reputation: 75

      //im also using yajra data tables..
      //it's easy to render html from controller...

      example: 
      $query = Appointment::all();
      $table = Datatables::of($query);
      $table->editColumn('user_id', function ($row) {
            return $row->user_id ? '<span style="color: green;">ACCEPTED</span>' : 
                                   '<span style="color: red;">PENDING</span>';           
            
        });
      //then you need to add the column you want to render html
       $table->rawColumns(['user_id']); 

Upvotes: 0

Akash khan
Akash khan

Reputation: 979

Try to feed data as JSON from the controller by using toJson() method.

$data= User::all();
return datatables()->of($data)
       ->addColumn('action', function ($row) {
          $html = '<a href="/users/'.$row->id.'">Edit</a> ';
          $html .= '<button data-rowid="'.$row->id.'">Del</button>';
          return $html;
        })->toJson();

Ref: https://laravelarticle.com/laravel-yajra-datatables

Upvotes: 0

Related Questions