PDA

View Full Version : [Solved] store callback & records count



amegahed
16 Jul 2009, 4:10 AM
Im trying to get the count of the records in a store that is built as follows:


// readerDLLinks
var readerDLLinks = new Ext.data.ArrayReader({}, [
{name: 'dlid'},
{name: 'dllink'},
{name: 'dlname'}
]);
// proxyDLLinks
var proxyDLLinks = new Ext.data.HttpProxy({
url: 'php/funcstf.php',
method: 'POST'
});
// dsDLLinks
var dsDLLinks = new Ext.data.Store({
reader: readerDLLinks,
proxy: proxyDLLinks
});


the data itself is returned from the php page as follows:


[["257","http://www.xyxyxyxy.com/forum/viewtopic.php?f=70&t=100731","Ghost Hunters International"],["2","http://www.xyxyxyxy.com/forum/viewtopic.php?f=70&t=124885","The Late Late Show with Craig Ferguson "],]

and im loading the store as follows:


function fnDLSRefresh(btn) {
if(btn == 'yes'){
dsDLLinks.load({
params : {action: 2},
callback: function(records, options, success){
alert(records[0].data.dlname);
}
});
}
};


now i've tried:
- alert(dsDLLinks.getCount()); ---- returns 0 always although there is data returned from the php page
- alert(records.getCount()); ---- error
finally i thought i would see if this "records" is reading anything by:
- alert(records[0].data.dlname);
which returns: records[0] is undefined

i used the same method to load data into a grid (from reading an array into the datastore), but im stuck here..

in short what i want to do is:
1. get the returned records count
2. will loop through that count and execute some AJAX request

thank u all

amegahed
16 Jul 2009, 5:06 AM
obviously there is something very stupid in my code that i can not notice and that everybody is just waiting for me to figure out :-?

Stripeman
16 Jul 2009, 5:10 AM
if i understand your question...this is normally done in your callback



$query = $db_session->sqlQuery($dataQuery);
$nrows = $db_session->num_rows($query);

while($rec = $db_session->fetch_array($query)) {
$arr[] = $rec;
};

$data = json_encode($arr);
$_REQUEST['callback'].'({"total":"'.$nrows.'","results":'.$data.'})';

and then in your store:



var dataReader = new Ext.data.JsonReader({
root: 'results',
totalProperty: 'total'
},fields
);

Stripeman
16 Jul 2009, 5:14 AM
actually... i run two queries for the paging.. the first one to get the total number of records.. and the second for the paging total

if you have questions about this let me know. I just didnt want to get off on another track.

steffenk
16 Jul 2009, 5:19 AM
try
console.info(records.length);
and for inspect
console.dir(records);

amegahed
16 Jul 2009, 5:40 AM
Thanks all for your help (thanks Stripeman for your pointer) i was going all manual building the array in php (for some reason i thought it easier to do, which really doesn't make any sense) so i changed the way the array is built and changed the store to:


// dsDLMovies dataStore
var dsDLLinks = new Ext.data.JsonStore({
totalProperty: 'total',
root: 'results',
url: 'php/funcstf.php',
method: 'POST',
fields: [
{name: 'dlid', type: 'int'},
'dllink', 'dlname'
]
});


and now the code works (im getting the first step which is the count back):


function fnDLSRefresh(btn) {
if(btn == 'yes'){
dsDLLinks.load({
params : {action: 2},
callback: function(records, options, success){
alert(records.length);
}
});
}
};

Stripeman
16 Jul 2009, 5:52 AM
glad u got it fingered out.

amegahed
16 Jul 2009, 5:56 AM
here is the php code:


while(.....some condition){
$arr[] = array('var1','var2','var3');
}

$data = json_encode($arr);
$totaldata = sizeof($arr);
echo '({"total":"'.$totaldata.'","results":'.$data.'})';


instead of doing this, i was writting the array manually, and without the last echo with the total and results....like this:



echo "[";
while(.....some condition){
echo "[\"".$val[0]."\",\"".$val[1]."\",\"".$val[2]."\"],";
}
echo "]";


plz, do not ask why i was doing it like this :) it's the only thing my grey cells could come up with at the time i wrote it (:|

steffenk
16 Jul 2009, 5:59 AM
why so complicate?


$return = array(
'total' => count($arr),
'results' => $arr
);
echo json_encode($return);

Stripeman
16 Jul 2009, 5:59 AM
here is the php code:


while(.....some condition){
$arr[] = array('var1','var2','var3');
}

$data = json_encode($arr);
$totaldata = sizeof($arr);
echo '({"total":"'.$totaldata.'","results":'.$data.'})';


instead of doing this, i was writting the array manually, and without the last echo with the total and results....like this:



echo "[";
while(.....some condition){
echo "[\"".$val[0]."\",\"".$val[1]."\",\"".$val[2]."\"],";
}
echo "]";


plz, do not ask why i was doing it like this :) it's the only thing my grey cells could come up with at the time i wrote it (:|


W.T.F. LOL

The wonderful thing about using PHP (and other languages) with Ext is that it saves you so much time from spelling so much out...

Stripeman
16 Jul 2009, 6:01 AM
why so complicate?


$return = array(
'total' => count($arr),
'results' => $arr
);
echo json_encode($return);

6 of 1 and a half dozen of the other :)

amegahed
16 Jul 2009, 6:19 AM
i seemed to come to another halt here, i changed my code to follow the same way (with the arrays) and now one of the grids show empty. i looked at the output of the store.load and it looks like this:


{"total":6,"results":[["\/storage\/drive4\/downloads\/(discografia) The Beach Boys (38 cds).rar","(discografia) The Beach Boys (38 cds).rar","1.618 GB"],["\/storage\/drive4\/downloads\/Agatha.Christies.Poirot.S11E01.Mrs.McGinty's.Dead.HDTV.XviD-BiA.[sharethefiles.com].avi","Agatha.Christies.Poirot.S11E01.Mrs.McGinty's.Dead.HDTV.XviD-BiA.[sharethefiles.com].avi","700.111 MB"],["\/storage\/drive4\/downloads\/Agatha.Christies.Poirot.S11E02.Cat.Amongst.the.Pigeons.HDTV.XviD-BiA.[sharethefiles.com].avi","Agatha.Christies.Poirot.S11E02.Cat.Amongst.the.Pigeons.HDTV.XviD-BiA.[sharethefiles.com].avi","700.162 MB"],["\/storage\/drive4\/downloads\/Apocalypse.Now.Redux.(1979).[sharethefiles.com].MDS","Apocalypse.Now.Redux.(1979).[sharethefiles.com].MDS","8.235 KB"],["\/storage\/drive4\/downloads\/ExtJS 2.2.rar","ExtJS 2.2.rar","9.971 MB"],["\/storage\/drive4\/downloads\/Warehouse.13.S01E03.Resonance.HDTV.XviD-FQM.[sharethefiles.com].avi","Warehouse.13.S01E03.Resonance.HDTV.XviD-FQM.[sharethefiles.com].avi","349.87 MB"]]}

the store is setup as follows:



// dsDLCompleted dataStore
var dsDLCompleted = new Ext.data.JsonStore({
totalProperty: 'total',
root: 'results',
url: 'php/mydownloads.php',
method: 'POST',
fields: [ 'filepath', 'filename', 'filesize']
});


and the grid's columnmodel like this:


// cmDLCompleted ColumnModel
var cmDLCompleted = new Ext.grid.ColumnModel([
smDLCompleted,
{id:'filename',header: 'Name', width: 270, sortable: true, dataIndex: 'filename'},
{header: 'Path', width: 300, hidden: true, sortable: true, dataIndex: 'filepath'},
{header: 'Size', width: 150, sortable: true, dataIndex: 'filesize'}
]);



i thought that the fields were already defined by the store's


fields: [ 'filepath', 'filename', 'filesize']

?

Stripeman
16 Jul 2009, 6:27 AM
did u


dsDLCompleted.load();

or



autoLoad: true


??

amegahed
16 Jul 2009, 6:29 AM
dsDLCompleted.load();

Stripeman
16 Jul 2009, 6:35 AM
how come you dont have any row headers/keys in your array ? (do you not need them for the jsonReader to parse the expected data?

Stripeman
16 Jul 2009, 6:36 AM
shoudl look something like this..



({"total":"","results":[{"0":29,"positionID":29,"1":"#18 Other Home Delivery Concessionaire Driver"
,"positionName":"#18 Other Home Delivery Concessionaire Driver"},{"0":32,"positionID":32,"1":"bibo"
,"positionName":"bibo"},{"0":25,"positionID":25,"1":"Custodial Worker","positionName":"Custodial Worker"
},{"0":26,"positionID":26,"1":"Medical Officer (Pediatrics)","positionName":"Medical Officer (Pediatrics
)"},{"0":28,"positionID":28,"1":"Renovation of building 7082","positionName":"Renovation of building
7082"},{"0":3,"positionID":3,"1":"Security Guard2ds3sd","positionName":"Security Guard2ds3sd"}]})

amegahed
16 Jul 2009, 6:41 AM
how come you dont have any row headers/keys in your array ? (do you not need them for the jsonReader to parse the expected data?

:) this exactly my question ... with the old method (my funny array method) the store looked like this:


// readerIMDB
var readerIMDB = new Ext.data.ArrayReader({}, [
{name: 'imdbid'},
{name: 'movietitle'},
{name: 'movieyear'}
]);
// proxyIMDB
var proxyIMDB = new Ext.data.HttpProxy({
url: 'php/mymovies.php',
method: 'POST'
});
// dsIMDB
var dsIMDB = new Ext.data.Store({
reader : readerIMDB,
proxy : proxyIMDB
});



and this read my funny array and everything showed up in my grid and so on, but now i changed that to :


// dsDLCompleted dataStore
var dsDLCompleted = new Ext.data.JsonStore({
totalProperty: 'total',
root: 'results',
url: 'php/mydownloads.php',
method: 'POST',
fields: [ 'filepath', 'filename', 'filesize']
});


and no more data in the grid? (plz don't mind the names of the stores, thay are different stores but i can not paste all the code here .. they follow the same idea in what they do)

what im guessing is that now i need to define the fields (headers - i do not necessairly have id's in all my queries - dont need to sometimes) .

amegahed
16 Jul 2009, 6:52 AM
ok ... my bad .. figured it out:


$fileinfo[] = array("filepath"=>$dir, "filename"=>$file, "filesize"=>$fsize);

that added the proper heading to the array :)

Stripeman
16 Jul 2009, 11:18 PM
Glad you got it worked out.. again lol