Laravel 5和DataTable JQuery错误

时间:2016-01-03 12:24:51

标签: jquery json laravel datatables

我使用服务器端jQuery DataTable来预览一些数据。在后端我使用Laravel 5.1框架。当我从服务器端返回JSON编码数据时,会发生以下错误:

  

警告4:参数请求未知...

我在服务器端检查了我的数据,一切都很干净。

以下是此问题的代码:

$result = \DB::table('table1')->select(\DB::raw($columns))
    ->whereRaw('somecondition')
    ->groupBy(\DB::raw($q))->get();

return [ "aaData" => $result ];

在我的函数返回这组结果后我使用:

return Response::json($response);

$response represents ["aaData" => $result]; // (set of data)

,客户端的代码是:

$table.dataTable({
    bProcessing: true,
    sAjaxSource: "{{url('/dashboard/getTableData')}}",
    "fnServerParams" : function(aoData) {
        aoData.push({name: 'name1', value: $('[name=name1]').val()});
        aoData.push({name: 'name2', value: $('[name=name2]').val()});
        aoData.push({name: 'name3', value: $('[name=name3]').val()});
        aoData.push({name: 'name4', value: $('[name=name4]').val()});
        aoData.push({name: 'name5', value: $('[name=name5]').val()});
        aoData.push({name: 'name6', value: $('[name=name6]').val()});
        aoData.push({name: 'name7', value: $('[name=name7]').val()});
        aoData.push({name: 'name8', value: $('[name=name8]').val()});
        aoData.push({name: 'name9', value: $('[name=name9]').val()});
        aoData.push({name: 'name10', value: $('[name=name10]').val()});
        aoData.push({name: 'name11', value: $('[name=name11]').val()});
        aoData.push({name: 'name12', value: $('[name=name12]').val()});
    },
    "fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
        console.log(aData);
        if(aData["col1"] === 'Yes') {
            $(nRow).css({"background-color":"#FFC2B2"});
        } else {
            $(nRow).css({"background-color":"#C2E0FF"});
        }
        return nRow;
    },
    aoColumns : [
        { "sWidth": "9%"},
        { "sWidth": "9%"},
        { "sWidth": "9%"},
        { "sWidth": "9%"},
        { "sWidth": "9%"},
        { "sWidth": "9%"},
        { "sWidth": "9%"},
        { "sWidth": "9%"},
        { "sWidth": "9%"},
        { "sWidth": "9%"},
        { "sWidth": "9%"}
    ]
});
};

有没有人知道如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

您应该检查$result的输出。 DataTable不接受关联数组或集合作为数据源。你应该遍历集合并获得唯一的值。

foreach($result as $item_set){
    $output["aaData"][] = [$item_set->col1, $item_set->col1, ...];
}

我希望它能奏效。