Laravel-datatables: Column buttons not working as expected

Created on 1 Jan 2019  路  4Comments  路  Source: yajra/laravel-datatables

Summary of problem or feature request

The Edit and Delete is working, but the Role Column buttons not working: Admin
image

This is my code:

// Controller
public function getUsers()
{
    $users = User::select(['id', 'name', 'email', 'phone', 'created_at']);

    return Datatables::of($users)
        ->editColumn('created_at', function ($user) {
            return $user->created_at->format('Y/m/d');
        })
        ->filterColumn('created_at', function ($query, $keyword) {
            $query->whereRaw("DATE_FORMAT(created_at,'%Y/%m/%d') like ?", ["%$keyword%"]);
        })
        ->addColumn('role', function ($user) {
            return '<a class="btn btn-primary" href="#">Admin</a>';
        })
        ->addColumn('action', function ($user) {
            return '<a href="'.route('admin.users.edit', $user->id).'" class="btn btn-xs btn-primary"><i class="fa fa-edit"></i> Edit</a>
            <a href="'.route('admin.users.destroy', $user->id).'" class="btn btn-xs btn-danger"><i class="fa fa-trash"></i> Delete</a>';
        })
        ->editColumn('id', 'ID: {{$id}}')
        ->make(true);
}
//View
@section('content')
<table id="users-table" class="table table-striped table-bordered" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th>Id</th>
            <th>Name</th>
            <th>Email</th>
            <th>Phone</th>
            <th>Member since</th>
            <th>Role</th>
            <th>Action</th>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>

<script>
    $(function() {
        $('#users-table').DataTable({
            processing: true,
            serverSide: true,
            ajax: '{!! route('admin.datatables.users') !!}',
            columns: [
                { data: 'id', name: 'id' },
                { data: 'name', name: 'name' },
                { data: 'email', name: 'email' },
                { data: 'phone', name: 'phone' },
                { data: 'created_at', name: 'created_at' },
                { data: 'role', name: 'role', orderable: false, searchable: false },
                { data: 'action', name: 'action', orderable: false, searchable: false },
            ]
        });
    });
</script>
@endsection

datatable result
image

  • Laravel Version 5.7.19
question

Most helpful comment

You should specify the raw columns that has html content.

Use ->rawColumns(['role', 'action'])

See https://yajrabox.com/docs/laravel-datatables/master/raw-columns for ref.

All 4 comments

I copy the content of column action
yet different result
image

It return me like this, the action column got it correct, but the role column is not correctly displayed.

{
  "draw": 0,
  "recordsTotal": 25,
  "recordsFiltered": 25,
  "data": [
    {
      "id": "ID: 1",
      "name": "Superadmin",
      "email": "[email protected]",
      "phone": null,
      "created_at": "2018/12/29",
      "roles": [
        {
          "id": "1",
          "name": "super-admin",
          "guard_name": "web",
          "created_at": "2018-12-29 02:50:58",
          "updated_at": "2018-12-29 02:50:58",
          "pivot": {
            "model_id": "1",
            "role_id": "1",
            "model_type": "App\\User"
          }
        }
      ],
      "role": "&lt;a href=&quot;http://ticket.test/admin/users/1/edit&quot; class=&quot;btn btn-xs btn-primary&quot;&gt;&lt;i class=&quot;fa fa-edit&quot;&gt;&lt;/i&gt; Edit&lt;/a&gt;\n            &lt;a href=&quot;http://ticket.test/admin/users/1&quot; class=&quot;btn btn-xs btn-danger&quot;&gt;&lt;i class=&quot;fa fa-trash&quot;&gt;&lt;/i&gt; Delete&lt;/a&gt;",
      "action": "<a href=\"http://ticket.test/admin/users/1/edit\" class=\"btn btn-xs btn-primary\"><i class=\"fa fa-edit\"></i> Edit</a>\n            <a href=\"http://ticket.test/admin/users/1\" class=\"btn btn-xs btn-danger\"><i class=\"fa fa-trash\"></i> Delete</a>"
    },
    ...

You should specify the raw columns that has html content.

Use ->rawColumns(['role', 'action'])

See https://yajrabox.com/docs/laravel-datatables/master/raw-columns for ref.

The role column from left join is cannot sort and searchable in frontend datatables? cause it's from the left join used on ->with('roles'),

public function getUsers()
{
    $users = User::select(['id', 'name', 'email', 'phone', 'created_at'])
                ->where('id','<>',1)
                ->with('roles');

    return Datatables::of($users)
        ->editColumn('created_at', function ($user) {
            return $user->created_at->format('Y/m/d');
        })
        ->filterColumn('created_at', function ($query, $keyword) {
            $query->whereRaw("DATE_FORMAT(created_at,'%Y/%m/%d') like ?", ["%$keyword%"]);
        })
        ->addColumn('role', function ($user) {
            return ucfirst($user->roles->first()->name);
        })
        ->addColumn('action', function ($user) {
            return '<a href="'.route('admin.users.edit', $user->id).'" class="btn btn-xs btn-primary"><i class="fa fa-edit"></i> Edit</a>
            <a href="'.route('admin.users.destroy', $user->id).'" class="btn btn-xs btn-danger"><i class="fa fa-trash"></i> Delete</a>';
        })
        ->editColumn('id', 'ID: {{$id}}')
        ->rawColumns(['role', 'action'])
        ->make(true);
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

vipin733 picture vipin733  路  3Comments

sangnguyenplus picture sangnguyenplus  路  3Comments

t0n1zz picture t0n1zz  路  3Comments

ghost picture ghost  路  3Comments

nasirkhan picture nasirkhan  路  3Comments