Yajra DataTables:无法在表中加载记录

问题描述 投票:0回答:1

我正在使用Yajra数据表包来加载记录。但是我在整页上而不是在特定表上获取记录。我不知道错误在哪里。

查看文件

<table id="clientsTable" class="table table-bordered table-striped dataTable">
    <thead>
        <tr>
            <th>#</th>
            <th>First name</th>
            <!--<th>Last name</th>-->
            <th>Mobile number</th>
            <th>Email</th>
            <th>Actions</th>
        </tr>
    </thead>
</table>

脚本文件

$(document).ready(function($) {
    $('#menu-clients').addClass('active');
        $('#clientsTable').DataTable({
            processing: true,
            serverSide: true,
            "bDestroy": true,
            "bAutoWidth": false,
            ajax:{
                url : '/clients',
                method: 'get',
            },
            columns:[
                { data: 'DT_RowIndex'},
                { data: 'first_name', name: 'first_name'},
                { data: 'mobile_no', name: 'mobile_no'},
                { data: 'email', name: 'email'},
                { data: 'actions', name: 'actions'},
            ]
        });
    });

路由文件

Route::resource('/clients', 'ClientsController');

控制器

public function index()
{
     $data = DB::table('clients')->orderBy('id', 'desc')->get();
     return Datatables::of($data)
     ->addColumn('actions','buttons.clients')
     ->rawColumns(['actions'])
     ->addIndexColumn()
     ->make(true);     
}

在网络标签中的响应

{draw: 0, recordsTotal: 166, recordsFiltered: 166, data: [,…], input: []}
draw: 0
recordsTotal: 166
recordsFiltered: 166
data: [,…]
[0 … 99]
[100 … 165]
input: []
jquery mysql laravel yajra-datatable
1个回答
0
投票

您必须使用Select查询,例如:

$data = DB::table('clients')->select('id','first_name','mobile_no','email')->orderBy('id', 'desc')->get();
     return Datatables::of($data)
     ->addColumn('actions','buttons.clients')
     ->rawColumns(['actions'])
     ->addIndexColumn()
     ->make(true);
© www.soinside.com 2019 - 2024. All rights reserved.