【问题标题】:Yajra DataTables : Unable to load records in tablesYajra DataTables:无法在表中加载记录
【发布时间】:2020-06-12 06:12:23
【问题描述】:

我正在使用 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: []

【问题讨论】:

  • 在控制台中显示错误 - 调用 ajax 时的网络
  • 我已通过网络响应编辑了我的问题

标签: jquery mysql laravel yajra-datatable


【解决方案1】:

您需要将视图和数据表数据的方法分开。

在您的索引方法中调用表的视图。

public function index()
{
     return view('yourTableBlade');    
}

同时删除 get() 方法 yajraDataTable 为您完成这项工作。

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

为您的数据表创建另一个路由。

Route::get('/tableData', 'ClientsController@tableData');

【讨论】:

    【解决方案2】:

    您必须使用 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);
    

    【讨论】:

    • 感谢您帮助我得到相同的结果。更改代码后。响应就像显示在整页上。不在桌子上
    • 我已经上传了我如何得到回复的图片,您现在可以查看
    猜你喜欢
    • 2021-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-05
    • 1970-01-01
    • 2019-03-02
    • 2014-12-15
    • 2018-08-15
    相关资源
    最近更新 更多