Laravel DataTables Yajrabox不显示数据

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

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

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

PHP版本:7.2.11 Laravel版本:5.7.19

以下是一些错误屏幕:

使用Artisan enter image description here在命令行上出错

HTML屏幕上的错误enter image description here

JSON错误:enter image description here

原始JSON,在Yajra Tables enter image description here的每个JSON响应的开始处显示“c”

这是我如何包括YajraTables enter image description here

控制器代码:

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 artisan yajra-datatable
1个回答
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();
 });
© www.soinside.com 2019 - 2024. All rights reserved.