1

I want to add action button to my datatable here the code html table and datatables

<table id="users-table" class="table table-bordered">
        <thead>
        <tr>
            <th>Id</th>
            <th>Name</th>
            <th>Email</th>
            <th>alamat</th>
            <th>nohp</th>
            <th>action</th>
        </tr>
        </thead>
    </table>

    <script id="script">
        $(function () {
            $('#users-table').DataTable({
                processing: true,
                serverSide: true,
                ajax: 'test/json'
            });
        });
    </script>

heres the laravel json datatables

public function data(Datatables $datatables)
{
    $builder = Kontak::query()->select('id', 'nama', 'email', 'alamat', 'nohp');

    return $datatables->eloquent($builder)
                    ->addColumn('action', 'users.datatables.intro')
                    ->rawColumns(['action'])
                    ->make();
}

but it keeps show result like this result images

Ersoy
  • 8,816
  • 6
  • 34
  • 48

2 Answers2

0

You should try ->escapeColumns([]) before ->addColumn('action', 'users.datatables.intro')

0

You can use the routes methods for your model.
view :

    <div class="table-responsive">
        <table class="table table-striped table-bordered">
            <thead>
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Email</th>
                <th>alamat</th>
                <th>nohp</th>
                <th>action</th>
            </tr>
            </thead>
            <tbody>
            @foreach($users as $user)
                <tr>
                    <td>{{ $user->id }}</td>
                    <td>{{ $user->name }}</td>
                    <td>{{ $user->email }}</td>
                    <td>{{ $user->alamat }}</td>
                    <td>{{ $user->nohp }}</td>
                    <td>
                        <form action="{{ route('users.destroy'  , ['id' => $user->id]) }}" method="post">
                            {{ method_field('delete') }}
                            {{ csrf_field() }}
                            <div class="btn-group btn-group-xs">
                                <a href="{{ route('users.edit' , ['id' => $user->id]) }}"  class="btn btn-primary">edit</a>
                                <button type="submit" id="deleteButton" data-name="{{ $user->id }}" class="btn btn-xs btn-warning">delete</button>
                            </div>
                        </form>
                    </td>
                </tr>
            @endforeach
            </tbody>
        </table>
    </div>

controller:

    public function index()
    {
        $users = User::latest()->paginate(25);
        return view('users.all' , compact('users'));
    }

change users.all to your users view.

Farid Vatani
  • 626
  • 1
  • 7
  • 24