【问题标题】:Laravel DataTables Yajrabox Not Displaying DataLaravel DataTables Yajrabox 不显示数据
【发布时间】:2019-08-01 12:17:57
【问题描述】:

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

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

PHP 版本:7.2.11 Laravel 版本:5.7.19

以下是一些错误屏幕:

使用 Artisan 的命令行出错

HTML 屏幕错误

JSON 错误:

原始 JSON 在 Yajra 表的每个 JSON 响应的开头显示“c”

这是我包含 YajraTables 的方式

控制器代码:

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>

请帮我解决这个问题。提前致谢。

【问题讨论】:

    标签: php laravel datatables laravel-artisan yajra-datatable


    【解决方案1】:

    改成

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

    【讨论】:

    • 我发现错误出现在 public/index.php 中,这里是截图:i.imgur.com/jlmTK2G.png 并查看输出:i.imgur.com/SlnKDdL.png
    • @SahilPasricha 但您仍然需要指定 rawColumns,您是否仍然遇到任何错误?
    • @SahilPasricha 您能告诉我,您使用的是哪个版本的 laravel 和 yajra 表吗?
    • @SahilPasricha 你可以看到here,它至少在 yajra table 8.0 中是静态方法,我正在使用。你也可以使用你的 ` Datatables::of(` ,我没用过,因为我没试过。
    • @SahilPasricha 注意我的回答,截图中没有get(),我可以看到你正在使用get()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-04
    • 1970-01-01
    • 2018-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多