Ad

Laravel 5 And DataTable JQuery Error

- 1 answer

I'm using server-side jQuery DataTable to preview some data. On backend I use Laravel 5.1 framework. When I return the JSON encoded data from the server side, the following error occurs:

Warning 4: Request Unknown for parameter ...

I have checked my data on server side and everything is clean.

Here is the code for this problem:

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

return [ "aaData" => $result ];

after my function return this set of result I use:

return Response::json($response);

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

and the code on client side is:

$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%"}
    ]
});
};

Does anyone have an idea how to fix this problem?

Ad

Answer

You should check your output for $result. DataTable won't accept associative array or collection as data source. You should loop through collection and get only values.

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

I hope it will work.

Ad
source: stackoverflow.com
Ad