KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

Datatable server side not accepting json value #1740

Open ankit raj opened this topic on on Nov 29, 2020 - 3 comments

ankit raj commented on Nov 29, 2020

Hi , i get this error as a popup while i use serverside request in datatable :

"DataTables warning: table id=datatables5fc37003da5d11 - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1"

But my json which the datatable request is receiving is a proper json and as given in the example for datatables :

{"draw":1,"recordsTotal":10,"recordsFiltered":10,"data":[["India","6"],["India","17"],["India","20"],["India","3"],["India","4"],["India","9"],["India","8"],["India","7"],["India","7"],["India","5"]]}

This is my code for initialising the datatable:

   \koolreport\datagrid\DataTables::create(array(
            "themeBase"=>"bs4",
            "serverSide"=>true,
            "ajax"=>'eventId=5',
            "cssClass"=>array(
                "table"=>"table table-striped table-bordered"
            ),
            "dataSource"=>$table_array,
               "showFooter"=>true,
            "options"=>array(
                "order" => [],
                "bInfo" => false,
                "dom" => 'Bfrtip',
                "buttons" => [
                    'csv', 'excel'
                ],
                "paging"=>count($table_array)>10,
                "pageLength"=>10,
                "pageIndex"=>0,
            )
        ));

If i don't give $table_array in datasource , i get koolreport error , datasource not defined.

Can you please help with how to achieve server side pagination/sorting etc because my json is perfectly proper as u can see in the example.

Sebastian Morales commented on Nov 30, 2020

Ankit, if you want to use ajax source directly with DataTables it's best not to set "serverSide" = true. "serverSide" is meant to use with a "dataSource" function. So in your case I would setup DataTables like this:

   \koolreport\datagrid\DataTables::create(array(
            "themeBase"=>"bs4",
            //"serverSide"=>true, don't use this together with "ajax"
            
            "cssClass"=>array(
                "table"=>"table table-striped table-bordered"
            ),
            "dataSource"=>$table_array,
               "showFooter"=>true,
            "options"=>array(
                "ajax"=>'eventId=5', //move "ajax" to inside "options" because this is a PHP widget, not a DataTables js object
                "order" => [],
                "bInfo" => false,
                "dom" => 'Bfrtip',
                "buttons" => [
                    'csv', 'excel'
                ],
                "paging"=>count($table_array)>10,
                "pageLength"=>10,
                "pageIndex"=>0,
            )
        )); 

Pls let us know the result. Cheers,

ankit raj commented on Nov 30, 2020

Hey Sebastian , I have a lot of records and i want server side pagination ,sorting etc, and i use an mvc model and hence i cant give a direct query in the datasource. Is there a way to work with the json response i get with the mvc model and get server side sorting ,pagination because the original datatables gets all its response from server side only without using datasource?

Sebastian Morales commented on Nov 30, 2020

I'm afraid not. DataTables' serverSide property utilizes SQL for paging, sorting, and filtering. If you data end point is JSON it's no use. As a guidance, I think it's never the best idea to perform paging, sorting, or filtering at the JSON level. That work should be left to your database which is built with that in mind.

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
help needed

DataGrid