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

2 Answers 2

0

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

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

Comments

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.

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.