1

我想在我的数据表中添加操作按钮,代码 html 表和数据表

<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>

这是 laravel json 数据表

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

但它会保持显示结果像这样的 结果图像

4

2 回答 2

0

您可以为模型使用路由方法。
看法 :

    <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>

控制器:

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

更改users.all为您的用户视图。

于 2020-06-18T07:31:22.500 回答
0

你应该->escapeColumns([])先 试试->addColumn('action', 'users.datatables.intro')

于 2020-06-18T07:24:57.077 回答