PDA

View Full Version : extjs 4, direct php proxy and grid paging



sarriss
30 May 2012, 2:42 AM
Hi Guys,

I have a direct proxy which returns a json encoded response (has total count in the response) it returns 300 records, i have pageSize set to 15 on the store, I have on application launch set the store to loadPage(1) but basically instead of loading just 15 it loads all 300? I have searched through the docs for days and im not getting anywhere, anyone have any ideas?




autoLoad: false,
proxy: {
type: 'direct',
api: {read:QueryOrganisation.getResults},
reader: {
type: 'json',
idProperty: 'id',
root: 'result',
totalProperty: 'totalCount'
}
},
sorters: {
direction: 'DESC',
property: 'id'
}

fschaeffer
30 May 2012, 9:25 PM
Your PHP backend should send only 15 records and the total property, stating how many records are available in total.

The grid will show all records from backend without taking your pageSize into account. the pageSize is only used to determine the navigation options in the paging toolbar...

sarriss
31 May 2012, 12:21 AM
thnx for the reply, ok so ive made my php sql statement contain



SELECT *, (select count(*) from organisations) as totalCount FROM organisations LIMIT ".$params->start.", ".$params->limit.""


which works fine, it starts at record 0 and limits in this case to 15, i copied totalCount out from the results and put into the json response as total to get records, the response is clean and looks good, 15 records with a count of 3000, the issue is now that the paging toolbar isnt taking note of anything, it says page 1 showing 1-15 of 15??? my reader is set to read the correct total from the response.

any further ideas

fschaeffer
31 May 2012, 3:59 AM
you told direct to use
totalCount as totalProperty, so your JSON-response should contain the totalCOunt:3000 ...

sarriss
31 May 2012, 5:05 AM
this is the type of response i get back, perhaps i have a wrong format

ive cut down resullts array to just 2 just to save time


[{
"type":"rpc",
"tid":1,
"action":"QueryOrganisation",
"method":"getResults",
"result":[{"id":"234911","userid":"3","status":"2","orgid":"234911","orgname":"AA H\/O"},{"id":"235081","userid":"3","status":"0","orgid":"235081","orgname":"Accenture"}],
"totalCount":3444
}]

yes i did specifiy the totalProperty to be totalCount but i do have that in the response???

scottmartin
31 May 2012, 10:34 AM
Your store and pagingtoolbar should both have a pagesize.
You should send start,limit to the server
You server should return the total records and the limited records

Have you verified your header is correct?
Is your return JSON sending all records back

Either the client is not sending the correct data, or the server is not responding correctly.

If you feel that all setup correctly, then provide a small working example of your problem

Scott.

sarriss
31 May 2012, 11:59 PM
hi, thnx for the reply i think im comfortable with things but better get them checked anyway

Here is my header



Request URL:
http://localhost/PRC/php/router.php
Request Method:
POST
Status Code:
http://www.sencha.com/forum/chrome-devtools://devtools/Images/successGreenDot.png200 OK
Request Headersview source

Accept:
*/*
Accept-Charset:
ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:
gzip,deflate,sdch
Accept-Language:
en,sv;q=0.8,en-GB;q=0.6,en-US;q=0.4
Authorization:
Basic bGFtcHA6ZXYxbHlvZGE=
Connection:
keep-alive
Content-Length:
113
Content-Type:
application/json
Cookie:
PHPSESSID=admfpbg7qgo6v4od8kh71f52s7
Host:
localhost
Origin:
http://localhost
Referer:
http://localhost/PRC/app2.html
User-Agent:
Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.106 Safari/535.2
X-Requested-With:
XMLHttpRequest

Request Payload

{"action":"QueryOrganisation","method":"getResults","data":[{"page":1,"start":0,"limit":5}],"type":"rpc","tid":1}

Response Headersview source

Connection:
Keep-Alive
Content-Length:
2758
Content-Type:
text/javascript
Date:
Fri, 01 Jun 2012 07:51:37 GMT
Keep-Alive:
timeout=5, max=95
Server:
Apache/2.2.17 (Unix) DAV/2 mod_ssl/2.2.17 OpenSSL/1.0.0c PHP/5.3.5 mod_apreq2-20090110/2.7.1 mod_perl/2.0.4 Perl/v5.10.1
X-Powered-By:
PHP/5.3.5



I have got the store to load page 2 which se the start to 5 (ive lowered the limit so i dont paste a lot of records :D

here is the full of what i get back


{"type":"rpc","tid":1,"totalCount":3444,"action":"QueryOrganisation","method":"getResults",
"result":[{"id":"234911","userid":"3","status":"2","orgid":"234911","orgname":"AA H\/O"},
{"id":"234961","userid":"3","status":"0","orgid":"234961","orgname":"Abbey Life Assurance Limited"},
{"id":"234964","userid":"3","status":"0","orgid":"234964","orgname":"Abbey National"},
{"id":"234966","userid":"3","status":"0","orgid":"234966","orgname":"Abbey National"},
{"id":"234968","userid":"3","status":"0","orgid":"234968","orgname":"Abbey National (Hill Samuel Investment Services)"}]}


my paging toolbar has the same pageSize as the store, autoload is off, on application launch i call the store.setPage(1); for that store, the data loads fine in the grid so its getting the result fine, i check the store and im not getting finding the total id expect on the store, proxy or reader, just shows 5, so it isnt reading the total in right. the reader is set to read totalCount? does all this look ok?

thnx again

scottmartin
1 Jun 2012, 2:32 PM
Here is an example I posted a few days for paging example with grid. It does not use direct, but you will get the general idea:
http://www.sencha.com/forum/showthread.php?211142 (post#5)

Scott.

sarriss
2 Jun 2012, 2:56 AM
Thnx for that, will check it out and let you know what my problem was, for the next person :)

segu
13 Jul 2012, 3:26 AM
Hi everybody. I solve this problem that sarriss discrabed above by adding that total property into result object, i.e. changed response from


action: "QueryDatabase"
method: "getResults"
result: [{ann_id:28, due_date:2012-03-06, annuity:9, }],
total: "750",
tid: 1,
type: "rpc"
to


action: "QueryDatabase"
method: "getResults"
result: {

data: [{ann_id:28, due_date:2012-03-06, annuity:9, }],


total: "750"
},
tid: 1,
type: "rpc"

and described in my proxy reader that the data items for grid should be in 'data' property.


proxy: {
type: 'direct',
api: {...},
reader: {
type: 'json',
root: 'data',
totalProperty: 'total'
}
}

sarriss
13 Jul 2012, 3:41 AM
thnx for that, i did try that with no avail, will try that again obviously, in the end i hacked open ext-all-debug.js and instead of getting it from ResultSet.total i got it from operation.response.total.

kinda hack and sort of messed with stuff but got my code working, hopefully yours is a more permenent and cleaner solution :D

squalo
25 Jul 2012, 12:09 AM
@segu:
I'm struggling with the same problem for days now.
Can you perhaps give an example (some code snippets ) of your PHP files QueryDatabase.php and router.php for your solution?
Thx

segu
25 Jul 2012, 2:30 AM
@segu:
I'm struggling with the same problem for days now.
Can you perhaps give an example (some code snippets ) of your PHP files QueryDatabase.php and router.php for your solution?
Thx

@squalo, the main idea is that we should pass to the router a parameter containing value of tolal number of records, then get him and save to result that both extjs paging toolbar and grid data store reader can read. But paging toolbar can find total property in that structure (only if it is separate):
result: {
data: [{ann_id:28, due_date:2012-03-06, annuity:9, }],
total: "750"
},

getResults method from QueryDatabase.php

public function getResults($params) {
...
// Get the total count for pagination
$sql = 'SELECT count(*) AS totalCount from annuity WHERE annuity.deleted = 0';
$total_result = $_db->query($sql);
$total_result = $total_result->fetch_assoc();
$results = array();
// Save to result all data
while ($row = $_result->fetch_assoc()) {
array_push($results, $row);
}
// Add the totalCount property to the end of results array
array_push($results, $total_result);
return $results;
}

doRpc method from router.php


function doRpc($cdata) {
...
$r['result'] = array();
$result = call_user_func_array(array($o, $method), $params);
// Here are my changes
$rr = (array)$result;
// Checking if result array contains at the end totalCount property
if (isset($rr[count($rr)-1]) AND isset($rr[count($rr)-1]['totalCount'])) {
// Get that value
$total = $rr[count($rr)-1];
// Delete it
array_pop($rr);
$r['result']['data'] = $rr;
// Write it to result
$r['result']['total'] = $total['totalCount'];
} else {
$r['result'] = $result;
}
// ---
doAroundCalls($mdef['after'], $cdata, $r);
...
return $r;
}

That's it, hope it helps you.

squalo
25 Jul 2012, 3:32 AM
You are my savior, my knight in shining armor. :)

the guys from sencha should add this in their Ext.direct Guide.

segu
25 Jul 2012, 3:58 AM
Woot? I hope that you are girl, because this is sounds a little strange for a guy ), anyways, I'm glad to help you.

squalo
25 Jul 2012, 6:50 AM
SORRY! Afraid, that I've seen too often Python's Holy grail ;)

But my struggle goes on - unforunately (like always with ExtJs) :(

Now I got an 'a is undefined' Error while saving some new stuff into the database. :((

Is there something else to consider in Router.php/ QueryDatabase.php reagrding createRecord() or updateRecords()?

segu
25 Jul 2012, 10:08 AM
Well, I think you could try to change this:


//getResults method from QueryDatabase.php
array_push($results, $row);

//doRpc method from router.php
$rr = (array)$result;
if (isset($rr[count($rr)-1]) AND isset($rr[count($rr)-1]['totalCount'])) {
// Get that value
$total = $rr[count($rr)-1];
// Delete it
array_pop($rr);
$r['result']['data'] = $rr;
// Write it to result
$r['result']['total'] = $total['totalCount'];
} else {
$r['result'] = $result;
}
to this:


//getResults method from QueryDatabase.php
$results += $total_result;

//doRpc method from router.php
if (is_array($result) && isset($result['totalCount']))
{
$total = array_pop($result);
$r['result']['data'] = $result;
$r['result']['total'] = $total;
} else {
$r['result'] = $result;
}

P.S.: So it was from the Holy Grail movie, unfortunately I have not watched it so often to take quotes from there. Need to watch it again. )

segu
18 Sep 2012, 2:07 AM
I have updated my crutch in previous post. It should work for CRUD now and mixed type of result variable, I guess. :-?