繁体   English   中英

Laravel DataTables Yajrabox 不显示数据

[英]Laravel DataTables Yajrabox Not Displaying Data

我最近几天一直面临这个问题,找不到任何解决方案。 我正在使用YajraTables ,作为 output JSON 结果,我在它呈现的每个 json 开始时得到“c”。

当我使用 php artisan 命令时,我也遇到同样的错误

PHP 版本: 7.2.11 Laravel 版本: 5.7.19

以下是一些错误屏幕:

使用 Artisan 在命令行上出错在此处输入图像描述

HTML 屏幕错误在此处输入图像描述

JSON 错误: 在此处输入图像描述

原始 JSON 在 Yajra 表的每个 JSON 响应的开头显示“c” 在此处输入图像描述

这是我如何包含 YajraTables 在此处输入图像描述

Controller 代码:

public function users(){

    $accounts = Accounts::select(['id', 'name', 'mobile', 'address', 'city'])->get();

    return Datatables::of($accounts)
        ->addColumn('action', function ($accounts) {
            return '<a href="bills/'.$accounts->id.'" class="btn btn-xs btn-primary btn-sm"><i class="glyphicon glyphicon-edit"></i> View Bills</a><a href="edit_accounts/'.$accounts->id.'" class="btn btn-xs btn-primary btn-sm"><i class="glyphicon glyphicon-edit"></i> Edit</a><a href="javascript:void(0);" class="btn delete_account btn-sm btn-xs btn-danger" data-id="'.$accounts->id.'"><i class="fa fa-trash-alt"></i> Delete</a>';
        })
        ->editColumn('id', 'ID: {{$id}}')
        ->make(true);

}

Javascript 代码:

$(function() {
    $('#users-table').DataTable({
        processing: true,
        serverSide: true,
        ajax: '{!! route('users') !!}',
        columns: [
            { data: 'name', name: 'name' },
            { data: 'mobile', name: 'mobile' },
            { data: 'address', name: 'address' },
            { data: 'city', name: 'city' },
            {data: 'action', name: 'action', orderable: false, searchable: false}
        ]
    });
});

HTML 代码:

<table class="table table-bordered" id="users-table">

                    <thead>

                    <tr>
                        <th>Name</th>
                        <th>Mobile</th>
                        <th>Address</th>
                        <th>City</th>
                        <th>Action</th>
                    </tr>

                    </thead>

                </table>

请帮我解决一下这个。 提前致谢。

将其更改为

public function users(){

    $accounts = Accounts::select(['id', 'name', 'mobile', 'address', 'city']);

     return DataTables::eloquent($accounts)
        ->addColumn('action', function ($account) {
            return '<a href="bills/'.$account->id.'" class="btn btn-xs btn-primary btn-sm"><i class="glyphicon glyphicon-edit"></i> View Bills</a><a href="edit_accounts/'.$account->id.'" class="btn btn-xs btn-primary btn-sm"><i class="glyphicon glyphicon-edit"></i> Edit</a><a href="javascript:void(0);" class="btn delete_account btn-sm btn-xs btn-danger" data-id="'.$account->id.'"><i class="fa fa-trash-alt"></i> Delete</a>';
        })
        ->editColumn('id', 'ID: {{$id}}')
        ->rawColumns(['action'])
        ->toJson();

}

如果您现在遇到任何错误,请告诉我。 添加 html 时,不要忘记指定原始列。

似乎您在某些地方添加了不必要的字符,但我希望您测试一下

->addColumn('action','<a href="bills/'.$accounts->id.'" class="btn btn-xs btn-primary btn-sm"><i class="glyphicon glyphicon-edit"></i> View Bills</a><a href="edit_accounts/'.$accounts->id.'" class="btn btn-xs btn-primary btn-sm"><i class="glyphicon glyphicon-edit"></i> Edit</a><a href="javascript:void(0);" class="btn delete_account btn-sm btn-xs btn-danger" data-id="'.$accounts->id.'"><i class="fa fa-trash-alt"></i> Delete</a>';
        );

检查它是否有效,如果你问我,我会把它放在刀片/部分中并用作

 ->addColumn('action', function(Video $account){
       return view('tagging.video-checkbox', compact('account'))->render();
 });

解决方案 1在您的项目中查找此文件

vendor/datatables/buttons.server-side.js

复制它并将其放入您的资产公用文件夹中,然后确保您之前将其作为抄写员包含在内

{{ $dataTable->scripts() }}

所以,它应该是这样的

<script src="{{ url('/') }}/assets/js/buttons.server-side.js"></script>
{{ $dataTable->scripts() }}

希望它会被修复。

解决方案 2移除按钮。 在数据表 class 中的构建方法中,通过添加类似这样的内容来删除按钮

 ->parameters([
        'dom' => 'Bfrtip',
        'buttons' => [],
    ]);

所以,最后的事情应该是这样的

public function html()
{
    return $this->builder()
                ->setTableId('users-table')
                ->columns($this->getColumns())
                ->minifiedAjax()
                ->dom('Bfrtip')
                ->orderBy(1)
                -->parameters([
                    'dom' => 'Bfrtip',
                    'buttons' => [],
                ]);
}

CONCLUSION如果还是报错,go检查一下,看有没有warning。 一切顺利!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM