PDA

View Full Version : ds.getCount() in a paged grid



sonic64
7 Apr 2007, 4:23 AM
Hello,

I am trying to retrieve the index of the last record in my grid. For this, I am using ds.getCount() to get the total number of records in my grid. But I am using a page grid with a limit of 12 records/page. So getCount returns always 12. How can I get the total number of records in all my datastore?

Thanks

tolstoj
7 Apr 2007, 4:45 AM
Hi
I would suggest th

sonic64
7 Apr 2007, 4:50 AM
Thanks but there is no refernce to what I am looking for....

I actually need the id of the last row in my dataset; Any suggestions?

Thanks

Animal
7 Apr 2007, 5:41 AM
The last row in the page, or the last row of the whole dataset when you may only be on page 1?

Lat row of the page is easily got from the Store.

Last row of the whole dataset would be quite easy too. Because you can send back any data you want from the server. You can send back all kinds of metadata. The Grid only uses what it's told to use. You can use the rest for your own purposes by getting the data object from the reader.

The JsonReader stores the returned data object in a property called "jsonData", the XmlReader stores the XML document in a property called "xmlData", so you can access any returned data you want.

sonic64
7 Apr 2007, 6:17 AM
Thanks Animal for your input..

if ds is my datastore ans totalcount is the total of my records in my json Data, then ds.reader.jsonData.totalCount gives me the total count.

I don't know what are the functions associated with my Json Reader. Can you please point me to the documentation for that?

Thanks.

Animal
7 Apr 2007, 6:42 AM
There are no methods of JsonReader that would be useful to you. It's used only by the Grid's Store object to pull Records out of the data object. It does keep a reference to the data object returned from the server though. See my post above. You can pull data directly out of that.

So, do you want the id of the last row in the page? Because that's easily got from the Store.

Or you you want the id of the last row in the dataset.

If you are using paging from a server, then those two things are different.

Id of the last row on the page would be just



myStore.getAt(myStore.getCount() - 1).id


If you wanted the id of the last row in the whole dataset, then you'd have to pass that back in your Json data from the server. Perhaps in a property called "lastIdInDataset", and you could just use



myJsonReader.jsonData.lastIdInDataset

sonic64
7 Apr 2007, 7:50 AM
Hello animal,

I actually used a combination of both:


var h = parseInt(ds.reader.jsonData.totalCount);
var d = ds.getAt(h).id


But I do get from h the index of the last row in my grid. But when I try to retrieve the id from that row therough ds.getAt I get an error saying: ds.getAt(h) has no properties!

I can't see where that is coming from

tryanDLS
7 Apr 2007, 7:55 AM
The array is 0-based. Try getAt(h-1).id

Animal
7 Apr 2007, 7:58 AM
Your grid is paged? And you return the total dataset size in your Json data block that you return from the server?

Store only caches the current page.

So if your dataset is 1000 rows, and your page size is 25, then ds.getAt(1000) is not going to return anything.

As I said, if you want the id of the last row in the whole dataset to be available to you at all times, then you are going to have to return that in your Json data block

sonic64
7 Apr 2007, 8:26 AM
Thank you Animal!!!!!

I finally got your suggestion.

What I did is go back to my php file that encodes my data into Json, and added a query:


$result = mysql_query("SELECT id FROM table ORDER BY id DESC");
$lastrow = mysql_fetch_assoc($result);


And in my json array, I did the following:


$mydata = array('myrecords' => $arr, 'totalCount' => $count, 'lastid' => $lastrow['id']);


then, simply in my js, I do:


ds.reader.jsonData.lastid (need to use parsInt() if use for calculations)


Thanks

JeffHowden
7 Apr 2007, 9:44 AM
var h = parseInt(ds.reader.jsonData.totalCount);
var d = ds.getAt(h).id


Be careful using parseInt() without specifying the base. It will attempt to guess the base to use when making the conversion. In some rare cases, it'll use 8 instead of 10. So, if you're really meaning for it to use base 10 all the time, force it by specifying it:


var h = parseInt(ds.reader.jsonData.totalCount, 10);
var d = ds.getAt(h).id

That said, in this case I don't think using parseInt() is even necessary as it should already be stored as an integer since the paging functionality of the grid will need it be an integer to function properly.


What I did is go back to my php file that encodes my data into Json, and added a query:

$result = mysql_query("SELECT id FROM table ORDER BY id DESC");
$lastrow = mysql_fetch_assoc($result);

Wouldn't this be a faster query (wouldn't require a full table scan)?


SELECT Max(id) FROM table

sonic64
7 Apr 2007, 9:53 AM
Yes Jeff you're right...your query is much faster..Thanks

Elfstone
8 Apr 2007, 7:55 AM
Why when I try to use

var h = ds.reader.jsonData.totalCount;

Firebug gives me the error
ds.reader.jsonData has no properties

The reader is defined as

var ds = new Ext.data.Store({
proxy: new Ext.data.HttpProxy({url: 'tutorial.php'}),
reader: new Ext.data.JsonReader({
root: 'tiposmaquina',
totalProperty: 'totalCount',
id: 'id'
},
[
{name: 'ID', mapping: 'id', type: 'int'},
{name: 'Nombre', mapping: 'nombre', type: 'string'}
]),
// turn on remote sorting
remoteSort: true
});

Any Idea? I

Animal
8 Apr 2007, 9:05 AM
Are you trying to access it before you've called ds.load()? Because then, obviously, jsonData will not have any properties.

Elfstone
8 Apr 2007, 9:36 AM
nope, i tried to do it before and after but it

Animal
8 Apr 2007, 9:48 AM
Loading is asynchronous.

Extract the value where you mean to use it, not immediately after you fire off a request to the server, and the server is still cranking away computing the response.

sonic64
8 Apr 2007, 10:02 AM
Elfstone, can you post your jsonData array. I'd like to see what it looks like. I've used almost the same code, and it's working fine

Elfstone
8 Apr 2007, 10:29 AM
Thanks Animal

Sonic this is for you, I know my code looks like yours. That