Chumper / Datatable

This is a laravel 4 package for the server and client side of datatables at http://datatables.net/
https://github.com/Chumper/Datatable
388 stars 153 forks source link

Secho showing null - json is displaying, but not datatable #217

Closed stumped101 closed 9 years ago

stumped101 commented 9 years ago

Sir, Like Rixhars ajazi posts earlier I am getting an draw (datatables v1.10 - modified for the chumper plugin as in your other responses) value of null. The json is displaying with a draw( or secho for previous versions) being returned as zero. I am using wampserver 2.4.9 with mysql.

Any ideas what is causing this? - any help greatly appreciated.

Thank you.

thienhung1989 commented 9 years ago

@stumped101 check by firebug (firefox) or development tool (chrome, firefox). Response to server side? Missed url?? Show code init of datatable.

stumped101 commented 9 years ago

Thanks for you response. My json response is: {"draw":0,"recordsTotal":15416,"recordsFiltered":15416,"data":[["Abelia Edward Goucher ","",25,30,2,0,"",0,0],["Abelia Edward Goucher ","",0,0,5,0,"",0,0],["Abelia grandiflora ","",30,40,2,0,"",0,0],["Abies alba ","European Silver-fir",30,50,0,0,"",0,0],["Abies concolor ","Colorado White-fir",300,350,0,0,"Multi(6)",0,0],["Abies Koreana ","",100,150,0,0,"RB",0,0],["Abies Koreana ","",200,250,0,0,"RB",0,0],["Abies koreana Silberlocke ","",40,50,5,0,"",0,0],["Abies koreana Silberlocke ","",25,30,2,0,"",0,0],["Abies noblis ","",100,150,0,0,"RB",0,0],["Abies nordmanniana ","Caucasian Fir",250,275,0,0,"Multi(5)",0,0],["Abies nordmanniana ","Caucasian Fir",250,275,0,0,"RB",0,0],["Acanthus mollis ","Bear's-breech",0,0,7.5,0,"",0,0],["Acer campestre ","Field Maple",0,0,0,0,"BR",10,12],["Acer campestre ","Field Maple",150,200,0,0,"Multi(3)",0,0],["Acer campestre ","Field Maple",60,90,0,1,"",0,0],["Acer campestre ","Field Maple",0,0,0,0,"RB",14,16],["Acer campestre ","Field Maple",90,120,0,0,"",0,0],["Acer campestre ","Field Maple",40,60,0,1,"",0,0],["Acer campestre ","Field Maple",0,0,0,0,"",10,12]..........,["wisteria floribunda ","",0,0,0,0,"",0,0]]}

Extract from view:

{{ Datatable::table()
->addColumn('id','Plant name','Common name','htmin','htmax','pmin','pmax','root','girthmin','girthmax')
->setUrl(URL::to('plants1'))
->render() }}

Routes.php:

Route::get('plants1', function(){ if(Datatable::shouldHandle()) {

    return Datatable::collection(Plant::all(array('Plant_name','Common_name', 'Htmin', 'Htmax',
        'Pmin', 'Pmax', 'root', 'girthmin', 'girthmax')))
    ->showColumns('Plant_name','Common_name', 'Htmin', 'Htmax',
        'Pmin', 'Pmax', 'root', 'girthmin', 'girthmax')
    ->searchColumns('Plant_name','Common_name', 'Htmin', 'Htmax',
        'Pmin', 'Pmax', 'root', 'girthmin', 'girthmax')
    ->orderColumns('Plant_name','Common_name', 'Htmin', 'Htmax',
        'Pmin', 'Pmax', 'root', 'girthmin', 'girthmax')
    ->make();
}

});

I am using dev tools in chrome. But no datatable is rendering - only json response.

Thanks for your help.

thienhung1989 commented 9 years ago

The field does not match! And Missed field 'id'.

Note: The fields are case sensitive!

->addColumn('id','Plant name','Common name','htmin','htmax','pmin','pmax','root','girthmin','girthmax')
Datatable::collection(Plant::all(array('Plant_name','Common_name', 'Htmin', 'Htmax',
        'Pmin', 'Pmax', 'root', 'girthmin', 'girthmax')))
    ->showColumns('Plant_name','Common_name', 'Htmin', 'Htmax',
        'Pmin', 'Pmax', 'root', 'girthmin', 'girthmax')

Avoid use 'collection'! Use the 'query' to optimize code. Example:

Datatable::query(Plant::query())->showColumns(array('Plant_name','Common_name', 'Htmin', 'Htmax',        'Pmin', 'Pmax', 'root', 'girthmin', 'girthmax'
            )
stumped101 commented 9 years ago

Thanks I made those changes to the fields as you recommended and also to the route ie query instead of collection - but no change. It still displays json, not datatable (there is html above the view extract shown above to link to datatables v.1.10 as in chumper guidelines on git).

thienhung1989 commented 9 years ago

:D Show me edited Code. Chumper works well with datatable 1.10 (currently, I'm using it). Surely, you edited something missing.

thienhung1989 commented 9 years ago

Common Errors:

  1. The columns do not match with each other (if using aoColumns the mData and mDataProp must be carefully).
  2. Name the initializing for each column is not in the response from server.
stumped101 commented 9 years ago

This is the edited code: Route::get('plants1', function(){ // if(Datatable::shouldHandle()) // {

    return Datatable::query(Plant::query())
    ->showColumns('Plant_name','Common_name', 'Htmin', 'Htmax',
        'pmin', 'pmax', 'root', 'girthmin', 'girthmax')
    ->searchColumns('Plant_name','Common_name', 'Htmin', 'Htmax',
        'pmin', 'pmax', 'root', 'girthmin', 'girthmax')
    ->orderColumns('Plant_name','Common_name', 'Htmin', 'Htmax',
        'pmin', 'pmax', 'root', 'girthmin', 'girthmax')
    ->make();

// }

});

View:(with angle brackets removed from html)

link rel="stylesheet" type="text/css" href="../DataTables-1.10.4/media/css/jquery.dataTables.css"
link rel="stylesheet" type="text/css" href="../DataTables-1.10.4/extensions/TableTools/css/dataTables.tableTools.min.css"
script type="text/javascript" src="../DataTables-1.10.4/media/js/jquery.js"/script script type="text/javascript" src="../DataTables-1.10.4/media/js/jquery.dataTables.js"/script script type="text/javascript" src="../DataTables-1.10.4/extensions/TableTools/js/dataTables.tableTools.js"/script

{{ Datatable::table()
->addColumn('Plant_name','Common_name','Htmin','Htmax','pmin','pmax','root','girthmin','girthmax')
->setUrl(URL::to('plants1'))
->render() }}

Are you using chumper with wampserver 2.4.9?

stumped101 commented 9 years ago

I used setAliasMapping() to see the response and this is an extract of the returned json - still no datatable unfortunately though:

{"draw":0,"recordsTotal":15416,"recordsFiltered":0,"data":[{"Plant_name":"Abelia Edward Goucher ","Common_name":"","Htmin":25,"Htmax":30,"Pmin":2,"Pmax":0,"root":"","girthmin":0,"girthmax":0},{"Plant_name":"Abelia Edward Goucher ","Common_name":"","Htmin":0,"Htmax":0,"Pmin":5,"Pmax":0,"root":"","girthmin":0,"girthmax":0},{"Plant_name":"Abelia grandiflora ","Common_name":"","Htmin":30,"Htmax":40,"Pmin":2,"Pmax":0,"root":"","girthmin":0,"girthmax":0},{"Plant_name":"Abies alba ","Common_name":"European Silver-fir","Htmin":30,"Htmax":50,"Pmin":0,"Pmax":0,"root":"","girthmin":0,"girthmax":0},{"Plant_name":"Abies concolor ","Common_name":"Colorado White-fir","Htmin":300,"Htmax":350,"Pmin":0,"Pmax":0,"root":"Multi(6)","girthmin":0,"girthmax":0},{"Plant_name":"Abies Koreana ","Common_name":"","Htmin":100,"Htmax":150,"Pmin":0,"Pmax":0,"root":"RB","girthmin":0,"girthmax":0},{"Plant_name":"Abies Koreana ","Common_name":"","Htmin":200,"Htmax":250,"Pmin":0,"Pmax":0,"root":"RB","girthmin":0,"girthmax":0},{"Plant_name":"Abies koreana Silberlocke ","Common_name":"","Htmin":40,"Htmax":50,"Pmin":5,"Pmax":0,"root":"","girthmin":0,"girthmax":0},{"Plant_name":"Abies koreana Silberlocke ","Common_name":"","Htmin":25,"Htmax":30,"Pmin":2,"Pmax":0,"root":"","girthmin":0,"girthmax":0},{"Plant_name":"Abies noblis ","Common_name":"","Htmin":100,"Htmax":150,"Pmin":0,"Pmax":0,"root":"RB","girthmin":0,"girthmax":0},{"Plant_name":"Abies nordmanniana ","Common_name":"Caucasian Fir","Htmin":250,"Htmax":275,"Pmin":0,"Pmax":0,"root":"Multi(5)","girthmin":0,"girthmax":0},{"Plant_name":"Abies nordmanniana ","Common_name":"Caucasian Fir","Htmin":250,"Htmax":275,"Pmin":0,"Pmax":0,"root":"RB","girthmin":0,"girthmax":0},{"Plant_name":"Acanthus mollis ","Common_name":"Bear's-breech","Htmin":0,"Htmax":0,"Pmin":7.5,"Pmax":0,"root":"","girthmin":0,"girthmax":0},{"Plant_name":"Acer campestre ","Common_name":"Field ..............................,{"Plant_name":"wisteria floribunda ","Common_name":"","Htmin":0,"Htmax":0,"Pmin":0,"Pmax":0,"root":"","girthmin":0,"girthmax":0}]}

thienhung1989 commented 9 years ago

@stumper1 Update Chumper/Datatable with version master.

file json wrong.

{"draw":0,"recordsTotal":15416,"recordsFiltered":15416,"data"

Must be

{"aaData":[...],"sEcho":2,"iTotalRecords":1,"iTotalDisplayRecords":1}
thienhung1989 commented 9 years ago
->setAliasMapping()

combine with option 'aoColumns' of datatable

->setOptions(
'aoColumns' => array(
                array(
                    // 'sTitle' => trans('management-system::units.titles.id') ,
                    // 'sClass' => 'text-center',
                    // 'sWidth' => '30pt',
                    // 'aDataSort' => array(
                    //     0,
                    //     1
                    // ) ,
                    'mData' => 'id'
                ) ,
                array(
                    'mData' => 'Plant_name'
                ) ,
                array(
                    'mData' => 'Common_name'
                ) ,
                array(
                    'mData' => 'Htmin'
                ) ,
                array(
                    'mData' => 'Htmax'
                ) ,
                array(
                    'mData' => 'Pmin'
                ) ,
                array(
                    'mData' => 'Pmax',
                ) ,
                array(
                    'mData' => 'girthmin',
                ) ,
                array(
                    'mData' => 'girthmax',
                )
            )
        )
     )

'setAliasMapping' call if you need. When call it, you must add options 'mData' or 'mDataProp' for Datatable. Default, Datatable will add data with none index.

I tested with response

{"draw":0,"recordsTotal":15416,"recordsFiltered":15416,"data":[["Abelia Edward Goucher ","",25,30,2,0,"",0,0],["Abelia Edward Goucher ","",0,0,5,0,"",0,0],["Abelia grandiflora ","",30,40,2,0,"",0,0],["Abies alba ","European Silver-fir",30,50,0,0,"",0,0],["Abies concolor ","Colorado White-fir",300,350,0,0,"Multi(6)",0,0]]}

and with '->setAliasMapping()' and 'aoColumns'

{"draw":0,"recordsTotal":15416,"recordsFiltered":0,"data":[{"Plant_name":"Abelia Edward Goucher ","Common_name":"","Htmin":25,"Htmax":30,"Pmin":2,"Pmax":0,"root":"","girthmin":0,"girthmax":0},{"Plant_name":"Abelia Edward Goucher ","Common_name":"","Htmin":0,"Htmax":0,"Pmin":5,"Pmax":0,"root":"","girthmin":0,"girthmax":0},{"Plant_name":"Abelia grandiflora ","Common_name":"","Htmin":30,"Htmax":40,"Pmin":2,"Pmax":0,"root":"","girthmin":0,"girthmax":0}]}

All ok!

I think you configured incorrect! I using 'Nginx'. Good luck!

stumped101 commented 9 years ago

I think you are right - not sure how to correct the webserver configuration - Many thanks for the help though - much appreciated.

stumped101 commented 9 years ago

Issue still not resolved - very dissapointed that chumper has not responded to my request for help on this.

Chumper commented 9 years ago

Did you check the other issues? One time there was a problem with nginx but since you are using windows there could also be a causes for this. Did you check if you can get any get parameter to work?

stumped101 commented 9 years ago

I altered BaseEngine to set secho to 1. This returned in the json as 1. I am using wampserver with apache 2.4.9. I presume since the secho was returned to me that the rewrite rule is not the issue - am I correct? I have the datatables working without the chumper plugin - but it is limited in what I can do. I would prefer to use the chumper plugin as I want more flexibility in rendering the datatables - any ideas what to do from here? Thanks

Chumper commented 9 years ago

I dont think this is a problem with datatable directly, if you call you page like sample.php?echo=17 can you access echo in the script and is its value 17?

I think the problem lies within the web server configuration as no get parameters can be accessed on the server.

stumped101 commented 9 years ago

This is what I get using sSide.php?draw=17 image

Chumper commented 9 years ago

What shows the page if you do

dd(Input::get('draw'));

?

stumped101 commented 9 years ago

I did that and it returned the json as before - for some reason the view is not being rendered - the json seems to be stuck in the routes code (....->make() as shown above) . I am not using View::make() etc. - I am just using the code provided in your git page.

Chumper commented 9 years ago

ok, now i am confused, which site do you try to access? Do you have two routes or one? Just to avoid confusions i would try it with one, where you put your call to View:make and above that the block with if(Datatable::shouldHandle()

stumped101 commented 9 years ago

I am using one route: Route::get('plants1', function(){ // if(Datatable::shouldHandle()) // {

    return Datatable::query(Plant::query())
    ->showColumns('Plant_name','Common_name', 'Htmin', 'Htmax',
        'Pmin', 'Pmax', 'root', 'girthmin', 'girthmax')
    ->searchColumns('Plant_name','Common_name', 'Htmin', 'Htmax',
        'Pmin', 'Pmax', 'root', 'girthmin', 'girthmax')
    ->orderColumns('Plant_name','Common_name', 'Htmin', 'Htmax',
        'Pmin', 'Pmax', 'root', 'girthmin', 'girthmax')
    ->setAliasMapping()
    ->make();

// }

});

shouldHandle is commented out as it did not work with it in. I tried to use two routes also but this returned the same json. In chrome dev tools the response that came back was - you are trying to document as json. The view code is plants1.blade.php(angle brackets removed): link rel="stylesheet" type="text/css" href="../DataTables-1.10.4/media/css/jquery.dataTables.css"
link rel="stylesheet" type="text/css" href="../DataTables-1.10.4/extensions/TableTools/css/dataTables.tableTools.min.css"
!-- jQuery -- script type="text/javascript" src="../DataTables-1.10.4/media/js/jquery.js"/script !-- DataTables -- script type="text/javascript" src="../DataTables-1.10.4/media/js/jquery.dataTables.js"/script !--link rel='stylesheet' type='text/css' href='Pp1.css'-- script type="text/javascript" src="../DataTables-1.10.4/extensions/TableTools/js/dataTables.tableTools.js"/script {{ Datatable::table() -addColumn('Plant_name') -addColumn('Common_name') -addColumn('Htmin') -addColumn('Htmax') -addColumn('Pmin') -addColumn('Pmax') -addColumn('root') -addColumn('girthmin') -addColumn('girthmax') -setUrl(URL::to('plants1')) -render() }}

Should there be View::make here, ajax call etc.?

Chumper commented 9 years ago

You do not call the View in your route, try this:

Route::get('plants1', function()
{
  if(Datatable::shouldHandle())
  {
    return Datatable::query(Plant::query())
      ->showColumns('Plant_name','Common_name', 'Htmin', 'Htmax', 'Pmin', 'Pmax', 'root', 'girthmin', 'girthmax')
      ->searchColumns('Plant_name','Common_name', 'Htmin', 'Htmax', 'Pmin', 'Pmax', 'root', 'girthmin', 'girthmax')
      ->orderColumns('Plant_name','Common_name', 'Htmin', 'Htmax', 'Pmin', 'Pmax', 'root', 'girthmin', 'girthmax')
      ->setAliasMapping()
      ->make();
  }
  return View::make("yourViewHere");
});
stumped101 commented 9 years ago

Thanks for that - it now displays the table headers but is telling me that the json is invalid - I checked it in jsonlint which said it was valid

Chumper commented 9 years ago

can you use datatables 1.9.4 as js?

stumped101 commented 9 years ago

I have been using 1.10, but if it works better with 1.9.4 I can download it

Chumper commented 9 years ago

i havent updated the serverpart yet to be compatible with 1.10 so 1.9.4 will be much more trouble free atm.

stumped101 commented 9 years ago

OK I will try that and let you know how it goes - thanks very much once again for your help

stumped101 commented 9 years ago

I tried 1.9.0 - telling me json is invalid again unfortunately

stumped101 commented 9 years ago

Just tried 1.9.4 json is still invalid - any suggestions on how to fix this

JSON:

{"aaData":[{"Plant_name":"Abelia Edward Goucher ","Common_name":"","Htmin":25,"Htmax":30,"Pmin":2,"Pmax":0,"root":"","girthmin":0,"girthmax":0},{"Plant_name":"Abelia Edward Goucher ","Common_name":"","Htmin":0,"Htmax":0,"Pmin":5,"Pmax":0,"root":"","girthmin":0,"girthmax":0}, ............................. "sEcho":0,"iTotalRecords":15416,"iTotalDisplayRecords":15416}

Chumper commented 9 years ago

try without ->setAliasMapping()

stumped101 commented 9 years ago

Same result unfortunately

stumped101 commented 9 years ago

These are the headers:

Accept:application/json, text/javascript, /; q=0.01 Accept-Encoding:gzip, deflate, sdch Accept-Language:en-US,en;q=0.8 Cache-Control:max-age=0 Connection:keep-alive Cookie:language=eyJpdiI6IndEY29LNmhvUHNtN3pMRGhsTUZtbEFyTlRUcjI5KytUSisyS2ZINkgrS0k9IiwidmFsdWUiOiJ1Uzd0VVVuSlNRWTNPcVNqRjFxMFphcVg5VlBmWTY0K1pCYXA1WGo5aVJFPSIsIm1hYyI6ImU1ODNhYzhjN2RlMTY0M2MxMmRjZGY5NjFkMmQ0MTEyY2UwZmVkMjJkMWRjMTcwMmMxOWI3NjRkYzc0ZmI5MGEifQ%3D%3D; laravel_session=eyJpdiI6Ikc3dVY5Ym9BWDUyN2YyZU90TTNyZ3c9PSIsInZhbHVlIjoiaFQ1MW1cL3d0YVpaS3poY2o3OHRjaVlHNW1Cdm1UQUtxZmM0MVwvSW9UR2J2V1RQZFVQTWpLbStLNXJXRFBuSzViZDBcL2t6bDJcL09SWERaV3JMS0VJK2hBPT0iLCJtYWMiOiI2M2UzY2QwMTUwZmI0NGY1ZTAyMTUxNmI0M2FlMzY0YmI2Njk4MDYyY2Y2YThjMTZhZTMyZTdiNTg4NThhNTcyIn0%3D Host:localhost Referer:http://localhost/WebPlants++/public/plants1 User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36 X-Requested-With:XMLHttpRequest Query String Parametersview sourceview URL encoded sEcho:1 iColumns:9 sColumns: iDisplayStart:0 iDisplayLength:10 mDataProp_0:0 mDataProp_1:1 mDataProp_2:2 mDataProp_3:3 mDataProp_4:4 mDataProp_5:5 mDataProp_6:6 mDataProp_7:7 mDataProp_8:8 sSearch: bRegex:false sSearch_0: bRegex_0:false bSearchable_0:true sSearch_1: bRegex_1:false bSearchable_1:true sSearch_2: bRegex_2:false bSearchable_2:true sSearch_3: bRegex_3:false bSearchable_3:true sSearch_4: bRegex_4:false bSearchable_4:true sSearch_5: bRegex_5:false bSearchable_5:true sSearch_6: bRegex_6:false bSearchable_6:true sSearch_7: bRegex_7:false bSearchable_7:true sSearch_8: bRegex_8:false bSearchable_8:true iSortCol_0:0 sSortDir_0:asc iSortingCols:1 bSortable_0:true bSortable_1:true bSortable_2:true bSortable_3:true bSortable_4:true bSortable_5:true bSortable_6:true bSortable_7:true bSortable8:true :1421529693431 Response Headersview source Cache-Control:no-cache Connection:Keep-Alive Content-Length:2304 Content-Type:text/html; charset=UTF-8 Date:Sat, 17 Jan 2015 21:21:33 GMT Keep-Alive:timeout=5, max=99

stumped101 commented 9 years ago

Eventually got it working -I omitted the return statement inside the shouldHandle() block. My mistake. Thanks to all who helped me on this