calculation on related foundset

Questions, tips and tricks and techniques for scripting in Servoy

calculation on related foundset

Postby Hans Nieuwenhuis » Sun Jun 17, 2012 1:20 pm

Hi,

I have been breaking my brain on this one :

I have a form with a tabbed form on it. (parent-child(s))

I need a calculation on the parent form to show the total sum of a column in the childs.

When I use the same relation as on the tabbed form, the calculation is
not correct when the users does a find/search in the child form.
Because if the users finds a subset of the related child records, then the calulation
is only done on this subset. Matematically correct, but not what I want.
I always want to show the total of all child record regardless if they are shown or not.

When I use a different relation ( same definition, other name), the calculation stays
correct even when the user does a find/search on the child records.
That makes sense, because the other name results in a different foundset.

But then I have another problem : when I create new child records, they are not part of
the "other" foundset, so the calculation does not get updated.

Has anyone come across this and found a solution ?

My current way around this is to use the use the "different relation name" and do a databaseManager.recalculate on the parent when the childs are saved.
That works, but it does not seem logical...

My calculation on the same relation as the tabbed form is :

Code: Select all
function calc_s() {
   var _tot = 0
   if (par1_to_chi1) {
      for (var i = 1; i <= par1_to_chi1.getSize(); i++) {
         _tot = _tot + par1_to_chi1.getRecord(i).aantal
      }
   }
   return _tot
}


My calculation on a different relation name is :

Code: Select all
function calc1() {
   var _tot = 0
   if (full_par1_to_chi1) {
      for (var i = 1; i <= full_par1_to_chi1.getSize(); i++) {
         _tot = _tot + full_par1_to_chi1.getRecord(i).aantal
      }
   }
   return _tot
}
Hans Nieuwenhuis
Betagraphics
http://www.deltics.nl
http://www.betagraphics.nl

Servoy Version 7.3.1
Java version 1.7.0.x
Database Oracle 11g
User avatar
Hans Nieuwenhuis
 
Posts: 1026
Joined: Thu Apr 12, 2007 12:36 pm
Location: Hengelo, The Netherlands

Re: calculation on related foundset

Postby jcompagner » Thu Jun 21, 2012 1:02 pm

if you want that the calculation is always up to date even with new records or updated records not yet saved to the database
Then your setup is not possible or at least not that easy
You could do a very tricky solution where you have both relations (so kind of the same relation but under different names like "my_normal_relation" and "my_normal_relation_always_all")
and then in the calculation you walk over both of them, and adding everything from the my_normal_relation + the once you then miss (looking at the pk or something from the all)
Johan Compagner
Servoy
User avatar
jcompagner
 
Posts: 8828
Joined: Tue May 27, 2003 7:26 pm
Location: The Internet

Re: calculation on related foundset

Postby Hans Nieuwenhuis » Thu Jun 21, 2012 2:29 pm

Thanks, Johan
I also thought of this way to do it.
When validating records, i also use 2 relations.
Why do you describe it as very tricky ?

Regards,
User avatar
Hans Nieuwenhuis
 
Posts: 1026
Joined: Thu Apr 12, 2007 12:36 pm
Location: Hengelo, The Netherlands

Re: calculation on related foundset

Postby jcompagner » Thu Jun 21, 2012 2:41 pm

you just have to compare exactly which record to get from what relation.
i guess you want to first start summing up the normal relation and then go over the all (if the all has a size() > normal) and you then have to extract the records that you missed from the normal..
Johan Compagner
Servoy
User avatar
jcompagner
 
Posts: 8828
Joined: Tue May 27, 2003 7:26 pm
Location: The Internet


Return to Methods

Who is online

Users browsing this forum: No registered users and 8 guests