【问题标题】:Laravel 5 and DataTable JQuery ErrorLaravel 5 和 DataTable JQuery 错误
【发布时间】:2016-01-03 12:24:50
【问题描述】:

我正在使用服务器端 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%"}
    ]
});
};

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

【问题讨论】:

    标签: jquery json laravel datatables


    【解决方案1】:

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

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

    我希望它会起作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-19
      • 2016-02-11
      • 2023-03-20
      • 1970-01-01
      • 2018-12-31
      • 1970-01-01
      • 1970-01-01
      • 2019-04-23
      相关资源
      最近更新 更多