2

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
    });
});

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0
      //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']); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.