-
14 Dec 2011 12:24 AM #1
Paging Grid with PHP backend
Paging Grid with PHP backend
Hey guys, I'm trying to implement a paging Ext.grid.Panel in my application.
I have successfully posted [Object { page=1, start=0, limit=25}] to my PHP function, and queried the correct results. But I am still unable to get the paging to work.
In the Ext.toolbar.Paging documentation it says that the server response must be in the following format:
http://docs.sencha.com/ext-js/4-0/#!/api/Ext.toolbar.Paging
However my server response is returned as:Code:{ "success": true, "results": 2000, "rows": [ // ***Note:** this must be an Array { "id": 1, "name": "Bill", "occupation": "Gardener" }, { "id": 2, "name": "Ben", "occupation": "Horticulturalist" }, ... { "id": 25, "name": "Sue", "occupation": "Botanist" } ] }
Here is my PHP code...{"type":"rpc","tid":4,"action":"QueryEmployee","method":"readEmployee",
"result":[{"emp_id":1,"emp_name":"Daniel Leng","emp_staff_no":".......
Code:public function readEmployee(stdClass $params) { $_db = $this->__construct(); $start = $params->start; $limit = $params->limit; $results = array(); if ($stmt = $_db->prepare( "SELECT " . "emp_id," . "emp_name," . "emp_staff_no," . "emp_email," . "emp_position," . "emp_datejoined," . "emp_username " . "FROM employee LIMIT ?, ?;")) { $stmt->bind_param('ii', $start, $limit); $stmt->execute(); $result = $stmt->get_result(); while ($row = $result->fetch_array(MYSQL_ASSOC)) { array_push($results, $row); } $stmt->close(); } return $results; }
How do I get my PHP function to return a response like the one mentioned above?
-
14 Dec 2011 2:07 PM #2Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 34,121
- Vote Rating
- 453
To have paging work, it needs to have the total number of results
Mitchell Simoens @SenchaMitch
Sencha Inc, Senior Forum Manager
________________
http://www.JSONPLint.com - Source to lint your JSONP!
Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
https://github.com/mitchellsimoens
Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/
Need more help with your app? Hire Sencha Services services@sencha.com
Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!
When posting code, please use BBCode's CODE tags.
-
14 Dec 2011 4:57 PM #3
Yes I am well aware of that... I could rewrite my PHP code to query the total number of results first, and then only query for the results. But how do I send the Total Number Of Results back to the client ?
My server response is always in this format... how do I add an extra parameter for the Total Number Of Results?{"type":"rpc","tid":4,"action":"QueryEmployee","method":"readEmployee",
"result":[{"emp_id":1,"emp_name":"Daniel Leng","emp_staff_no":".......
-
15 Dec 2011 6:27 AM #4Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 34,121
- Vote Rating
- 453
It needs to be a level above the data:
Code:{"type":"rpc","tid":4,"action":"QueryEmployee","method":"readEmployee", total : 100, "result":[{"emp_id":1,"emp_name":"Daniel Leng","emp_staff_no":".......Mitchell Simoens @SenchaMitch
Sencha Inc, Senior Forum Manager
________________
http://www.JSONPLint.com - Source to lint your JSONP!
Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
https://github.com/mitchellsimoens
Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/
Need more help with your app? Hire Sencha Services services@sencha.com
Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!
When posting code, please use BBCode's CODE tags.
-
15 Dec 2011 7:42 PM #5
I managed to modify Router.php and got my JSON format returned like this:
But i still can't get the paging toolbar to show the number of results.{"type":"rpc","tid":4,"action":"QueryEmployee",
"method":"readEmployee",
"results":"22",
"result":[{"emp_id":1,"emp_name":"Daniel Leng"
I called store.load() with the following parameters:
My PHP function correctly calls for 20 records. But the total number of records in actually 22... so I expect the paging toolbar to showCode:this.store.load({ Params: { start: 0, limit: 20 } });
But it shows"Showing records 1- 20 of 22"
It doens't know that there is more than 20 records."Showing recods 1-20 of 20"
And since it doesn't know that there is more than 20 records, I can't get it to page.
-
18 Dec 2011 5:08 PM #6
-
19 Dec 2011 4:22 AM #7
Try format
insted ofCode:"results":22
Remove quotes from numbers.Code:"results":"22"
-
20 Dec 2011 12:44 AM #8
have you specified the 'totalProperty' in your Direct Store? It defaults to 'total', so this will not work until you've set totalProperty: 'results' in your direct store.
By default the name is 'total'. You could also just change the results key to 'total' instead of 'results'.
You should not need to modify router.php to get this working - I use remote paging w/ direct and it works fine.
See:
http://docs.sencha.com/ext-js/4-0/#!/api/Ext.data.reader.Reader-cfg-totalProperty
-
20 Dec 2011 12:53 AM #9
Tried it, casted the value to an integer. Still not getting it to page.

Any other tips ? This is just strange.{"type":"rpc",
"tid":4,
"action":"QueryEmployee",
"method":"readEmployee",
"results":1000,
"result":[{"emp_id":1,"emp_name":"Daniel Leng","emp_staff_no":"8888"....
-
20 Dec 2011 1:10 AM #10
Hello Erikvp thank you for your reply. I missed your post earlier.
Well I'm not using ajax requests so I thought readers would be useless for me... You need to specify a reader in order to set the totalProperty config right ? This is how I specify my proxy in my Store:
Where should I be adding the reader ?Code:proxy: { type: 'direct', api: { //client-side function : server-side function. create: QueryEmployee.createEmployee, read: QueryEmployee.readEmployee, destroy: QueryEmployee.destroyEmployee } }
The reason why I had to modify my router.php is because its not returning the JSON in the format that is required by the paging toolbar. It does not return "total" and "success", for that matter.


Reply With Quote