数据不会显示在我的数据表中

时间:2018-04-05 09:11:09

标签: ajax datatable laravel-5.2

美好的一天。有人可以帮助我,如果我在返回语句中使用下面的代码

,数据将不会显示在数据表中
  

$ unpaid = ['1','2','3','4']; //样品

     

$ paid = Datatables :: of($ query) - > make(true) - > getData();

     

return ['paid'=> $ paid,'unpaid'=> $未付];

响应

{paid: {draw: 0, recordsTotal: 2, recordsFiltered: 2,…}, unpaid: ["1", "2", "3", "4"]}
paid:{draw: 0, recordsTotal: 2, recordsFiltered: 2,…}
data:[{product_name: "Grading 123", full_name: "Sample, Tester", created_at: "2018-03-06 15:49:30",…},…]
0:{product_name: "Grading 123", full_name: "Sample, Tester", created_at: "2018-03-06 15:49:30",…}
  created_at:"2018-03-06 15:49:30"
  full_name:"Sample, Tester"
  level:"organization"
  product_name:"Grading 123"
1:{product_name: "Sample Grading ", full_name: "Sample, Test", created_at: "2018-03-16 13:14:19",…}
  created_at:"2018-03-16 13:14:19"
  full_name:"Sample, Test"
  level:"school"
  product_name: "Sample Grading "
draw:0
input: {level_id: "57", level: "school", _: "1522918726519"}
queries: [{,…}, {,…}]
recordsFiltered:2
recordsTotal:2
unpaid:"1", "2", "3", "4"]

错误

Uncaught TypeError: Cannot read property 'length' of undefined
 at jquery.dataTables.min.js:48
 at i (jquery.dataTables.min.js:35)
 at Object.success (jquery.dataTables.min.js:35)
 at j (jquery.js:3099)
 at Object.fireWith [as resolveWith] (jquery.js:3211)
 at x (jquery.js:8264)
 at XMLHttpRequest.<anonymous> (jquery.js:8605)

但如果代码是这样的

  

$ paid = Datatables :: of($ query) - &gt; make(true);

     

返回$ paid;

使用下面的代码,数据将成功显示在我的数据表中。

的Ajax

$(function(){
    viewStudent = $('#viewStudent-table').DataTable({
        order: [[ 0, "asc" ]],
        Language: {
            sLoadingRecords:"<img src='{{asset('assets/images/load.gif')}}' style='max-height:30px;'/> Fetching data...",
            sEmptyTable: "No data available"
        },
        ajax:{
            url:'{{ route("json_product_user") }}',
            data: function (d) {
                d.level_id = level_id;
                d.level = level;
            }
        },
        columnDefs: [
            {
                targets: 0,
                searchable:false,
                orderable:false,
            },
        ],
        columns:[
            {
                "className":      'details-control',
                "orderable":      false,
                "searchable":     false,
                "data":           null,
                "defaultContent": ''
            },
            {
                data: "full_name",
                render: function(data, type, row){
                    return data;
                }
            },
            {
                data: "product_name",
                render: function(data, type, row){
                    var html = '';
                    html += '<p style="">'+data+'</p>';
                    return html;
                }
            }
        ]
    });

这是我的控制器。

public function viewReadyToGradePaid(Request $request)
{
    if( $request->has('level_id') ){
        if($request->level == 'school') {

            $school_students= School::find($request->level_id);
            $students_list= $school_students->students()->active()->get();

            $query= DB::table('product_user')
                ->leftJoin('products', 'product_user.product_id', '=', 'products.id')
                ->leftJoin('school_products', 'product_user.product_id', '=', 'school_products.product_id')
                ->leftJoin('users', 'product_user.user_id', '=', 'users.id')
                ->whereRaw("products.service_sub_type_id = 2 AND (school_products.school_id = " . $request->level_id . " OR products.organization_id = ".School::find($request->level_id)->organization_id . ")")
                ->select('products.name as product_name', DB::raw('CONCAT(users.last_name, ", ", users.first_name ) AS  full_name'), 'product_user.created_at', 'products.level as level');
//                    ->get();
        }
        else{
            $query= DB::table('product_user')
                ->leftJoin('products', 'product_user.product_id', '=', 'products.id')
                ->leftJoin('school_products', 'product_user.product_id', '=', 'school_products.product_id')
                ->leftJoin('users', 'product_user.user_id', '=', 'users.id')
                ->whereRaw("products.service_sub_type_id = 2 AND products.level = 'organization' AND products.organization_id = ".$request->level_id)
                ->select('products.name as product_name', DB::raw('CONCAT(users.last_name, ", ", users.first_name ) AS  full_name'), 'product_user.created_at', 'products.level as level');
//                    ->get();
        }
    }
    $unpaid= ['1', '2', '3', '4']; //I Will get the list of students with UNPAID status.
    $paid= Datatables::of($query)->make(true)->getData();
    return ['paid' => $paid, 'unpaid' => $unpaid];
}

0 个答案:

没有答案