PDA

View Full Version : manage data base



tonin10
21 Feb 2011, 8:12 AM
Hello,
i have a data base to calcul route


Ext.regModel('trajet',{
fields : ['id','date', 'depart','arrivee','distance','note'] ,
proxy: {
type: 'localstorage',
id : 'bdd_listeStore'
}
});
var bdd_liste = new Ext.data.Store({
model: 'trajet',
autoLoad:true,
storeId: 'bdd_route' ,
getGroupString : function(record) {
return record.get('date').substr(0,7);// date are in format 2011-01-31
}


});


my app calcul distance you have made in a month. I want put in group bar( in my Ext.list) years and month and the total distance of the month
for the moment i know how made years and month but how can calculate total distance by month?

thanks a lot for your help

antonin

tonin10
22 Feb 2011, 5:14 AM
ok, i found a solution
thanks to Mitchell Simoens and it's Ext.ux.touch.GridSummary

in my data store


getGroupString : function(record) {
var mois = record.get('date').substr(0,7);
var distance = getTotalDistance(mois);
return mois +' total : '+distance +' km';
}


and my function

getTotalDistance =function(mois)
{
var amount = 0;
var search = new RegExp(mois);
bdd_liste.each(function(record){
var value = record.get('date');
if(value.match(search)){
amount += record.get('distance');
}
});



return amount
}

but i have a question, isn't possible to do the same like mysql SUM(col) ?
because, if i have a lot of data it's not very optimise to read all data

thanks

gcallaghan
22 Feb 2011, 10:02 AM
Check out Ext.data.Store.sum
http://dev.sencha.com/deploy/touch/docs/source/Store.html#method-Ext.data.Store-sum

Although it, just like MySQL sum, still need to iterate over all the records.

In the above code, it looks like you are iterating n^2 times over your records array. I would cache the total whenever a record is changed.

i.e...



setTotal = function(){
this.totalDistance = bdd_liste.getStore().sum('distance');
},
getTotal = function(){
return this.totalDistance;
}
listeners:{
datachanged:function(){ // this must be bubbled from the store and scoped appropriately
this.setTotal();
}
}

Riaz
22 Feb 2011, 4:27 PM
Nice solution. Thanks both for sharing your ideas.

tonin10
23 Feb 2011, 12:18 PM
Sorry for my answers so late
Thanks for for your response
I think for my app it s not necessery but usefull tips
In a futur post i made a wip with this app, you Will see all
Thanks

Ps: this post can be close