【问题标题】:Yajra datatable is not loading data properly in datatableYajra 数据表未在数据表中正确加载数据
【发布时间】:2019-05-13 05:04:23
【问题描述】:

可能是重复的,但我没有找到解决问题的合适方法.. 实际问题是 ajax 调用运行良好并返回表的响应数据,但表始终只显示加载.. 这里

用户控制器.php

if($request->ajax())
        {
            $users = User::select('id','name','email','phone','created_at','updated_at')->where('id','!=',Auth::user()->id)->get();

            return DataTables::of($users)
                ->editColumn('action', function ($user) {

                        return ' <a class="icon-color"  href="'.route('users.edit',$user->id).'"><i class="fa fa-edi" aria-hidden="true"></i></a>

                        <a class="icon-color"  href="'.route('users.show',$user->id).'"><i class="fa fa-eye" aria-hidden="true"></i></a>

                        <a class="icon-color"  href="'.route('users.destroy',$user->id).'"><i class="fa fa-trash" aria-hidden="true"></i></a>';

                })
                ->rawColumns(['action'])
                ->make(true);
        }

        return view('manager.users.index');

index.blade.php

<table id="users-table" class="table table-striped table-no-bordered table-hover" >
                            <thead style="font-size: 12px;" class="text-primary">
                            <tr>
                                <th style="text-align: center" width="5%">#</th>
                                <th style="text-align: center" width="20%">Name</th>
                                <th style="text-align: center;" width="25%">Email</th>
                                <th style="text-align: center;" width="10%" >Phone</th>
                                <th style="text-align: center;" width="15%" >Created Date</th>
                                <th style="text-align: center;" width="15%" >Updated Date</th>
                                <th style="text-align: center;" width="10%">Action</th>
                            </tr>
                            </thead>

                            <tbody>

                            </tbody>
                        </table>

脚本

  $('#users-table').DataTable({
            "lengthMenu": [
                [10, 25, 50, -1],
                [10, 25, 50, "All"]
            ],
            processing: true,
            serverSide: true,
            responsive: true,

            ajax:{
                url:"{{route('users.index')}}",
                type:'get',
                success:function(xhr){
                    toastr.success('Completed!')
                                        }
            },
            columns: [
                {data:'id',name:'id'},
                {data: 'name', name: 'name'},
                {data: 'email', email: 'email'},
                {data: 'phone', name: 'phone'},
                {data: 'created_at', name: 'created_at'},
                {data: 'updated_at', name: 'updated_at'},
                {data: 'action', searchable:false},

            ],
            "order":[[0,'desc']]
        });

在这里,我捕获了请求响应的短屏幕

任何能够找到解决方案的人.. 任何治疗都将不胜感激。谢谢..

【问题讨论】:

  • 从你的表格中移除 tbody 标签。
  • 我试过那个但没有用..

标签: php ajax laravel datatables


【解决方案1】:

更改 ajax 调用并从 ajax 中删除 typesuccess 属性。

ajax:{
   url:"{{route('users.index')}}",
   type:'get',
   success:function(xhr){
     toastr.success('Completed!')
   }
},

进入

ajax: '{{ route('users.index') }}',

我希望这会有所帮助。其余代码正确。

【讨论】:

  • 我能够从调用中获得响应,但数据没有加载到表中。你可以从图像中看到它。
  • 您可以更改您的 ajax 调用并检查数据是否加载到表中
【解决方案2】:
$('#users-table').DataTable({
            "lengthMenu": [
                [10, 25, 50, -1],
                [10, 25, 50, "All"]
            ],
            processing: true,
            serverSide: true,

            ajax:{
                url:"{{route('users.index')}}",
            },
            columns: [
                {data: 'id', name:'id'},
                {data: 'name', name: 'name'},
                {data: 'email', name: 'email'},
                {data: 'created_at', name: 'created_at'},
                {data: 'updated_at', name: 'updated_at'},
                {data: 'action'},

            ],
            "order":[[0,'desc']]
        });

只是 ajax 调用的问题

【讨论】:

  • 这不是问题,您可以指定调用类型获取或发布没有问题
猜你喜欢
  • 2023-03-12
  • 2015-04-09
  • 2018-11-14
  • 2018-08-14
  • 1970-01-01
  • 2019-02-15
  • 1970-01-01
  • 2021-12-31
  • 1970-01-01
相关资源
最近更新 更多