dqoag62688 2017-07-19 11:31
浏览 107
已采纳

如何使用Laravel 5.4显示来自Datatables的数据?

I'm stuck with datatables and the plugin yajrabox for Laravel 5.4. The goal is to load with ajax and the plugin the data from the users tables in my database, but it just shows me this error :

DataTables warning: table id=listingUsers - Requested unknown parameter '0' for row 0, column 0. 

I can't find out where it's coming from and i'm not sure I wrote the code right..

Here is my code.

Controller :

 public function index() {
    $users = User::latest()->count();

    return view('admin.users.index', compact('users'));
}

 public function ajaxListing() {
    $users = User::select(['id', 'username', 'email']);
        return Datatables::of($users)->make(true);
}

Routes :

Route::resource('users', 'Admin\UsersController');
Route::any('user-data', 'Admin\UsersController@ajaxListing')->name('datatables.data');

View : `

<table class="table table-bordered table-responsive" id="listingUsers">
    <thead>
        <th>ID</th>
        <th>Nom</th>
        <th>Email</th>
    </thead>

    <tbody></tbody>
</table>

@push('scripts')
<script>
    $(document).ready(function () {
        $('#listingUsers').DataTable({
            processing: true,
            serverSide: true,
            ajax: '{!! route('datatables.data') !!}',
            columns: [
                {data: 0, name: 'id'},
                {data: 1, name: 'name'},
                {data: 2, name: 'email'}
            ]
        });
    });

</script>
@endpush`

WHen i'm searching the error in the console and the network the data comes right but just doesn't show in the table.

Can someone tell me what t'im doing wrong and how to fix it ?

  • 写回答

1条回答 默认 最新

  • douyanjing0822 2017-07-19 12:11
    关注

    In data, you need to specify the column name, like that:

    columns: [
        {data: 'id', name: 'id'},
        {data: 'username', name: 'username'},
        {data: 'email, name: 'email'}
    ]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同