Home
The Toolkit for Online Communities
12611 Community Members, 0 members online
Log In Register

OpenACS BlogArchive

OpenACS Home : OpenACS Blog : Archive : 2007 : March

Sub-totalling with List Builder

Sub-totalling with List Builder is a great feature and I would definitely recommend using it over hand-coding your sub-totals but isn't fully documented, and there are a few gotchas, so I've put a few notes together to help you use it.



Let's just take a simple list with 2 columns: stage_description and financial_amount. We want to aggregate financial_amount for each stage_description. So in the beginning the elements section of our list looks like this:

  -elements {
          stage_description {
            label "[_ payment.Financial_Stages]"
          }
          financial_amount  {
            label "[_ payment.Amount]"
          }
    }





1. First we create a variable named groupby – give it a value of the name of the element we want to group by. Put this before our list definition. Behind the scenes this tells List Builder to create a filter named "groupby" (according to the docs you may need to put this in your ad_page_contract – I didn't need to):

set groupby "stage_description"



2. Add a new groupby section to end of the list. "type multivar" just tells List Builder to tell ad_page_contract that the groupby is not a multiple element. All the curly brackets are unfortunately needed – the first list element is the text which is displayed in front of each group; you can put HTML tags and more in here. The second list element tells List Builder that we are grouping by stage_description – this seems repetitive so maybe there’s a better way to do this, but I can't see any issues with it. You can also have an orderby clause in here if you need one.

  -groupby {
      type multivar
      values {
        { {[_ payment.Financial_Stage]} { {groupby stage_description} } }
      }
  }



3. Modify the financial_amount element to add aggregation information. aggregate can be one of “sum”, “average” or “count”. The aggregate_group_label allows you to put some text and a currency symbol before each sub-total. aggregate_label does the same for the grand total.

          financial_amount {
            label "[_ payment.Amount]"
            aggregate "sum"
            aggregate_group_label "[_ payment.Sub-Total]: [lc_get -locale $locale currency_symbol]"
            aggregate_label "[_ payment.Grand-Total]: [lc_get -locale $locale currency_symbol]"
          }



4. Gotchas to look out for:

you need to  use the -pass_properties section to allow List Builder to see program variables outside the list scope e.g. $locale.



List builder doesn't differentiate between currency and numbers – it just treats the aggregated column as a number. However, it does use the acs-lang formatting when displaying (specifically lc_numeric), which should be good enough for most cases. You need to do your own formatting on the individual line items. Here's how to use display_eval to format the financial_amount element:

display_eval {[lc_monetary -label_p "t" $financial_amount $locale ]}



Noquoting can be painful but the following workaround should suffice – note the triple underscore:

display_template {@financial.financial_amount___display;noquote@}




Our finished example:

  -elements {
          stage_description {
            label "[_ payment.Financial_Stages]"
          }
          financial_amount {
            label "[_ payment.Amount]"
            aggregate "sum"
            aggregate_group_label "[_ payment.Sub-Total]: [lc_get -locale $locale currency_symbol]"
            aggregate_label "[_ payment.Grand-Total]: [lc_get -locale $locale currency_symbol]"
display_eval {[lc_monetary -label_p "t" $financial_amount $locale ]}
display_template {@financial.financial_amount___display;noquote@}
          }
    }
  -groupby {
      type multivar
      values {
        { {[_ payment.Financial_Stage]} { {groupby stage_description} } }
      }
  }





A good example from OpenACS CVS:

http://cvs.openacs.org/cvs/openacs-4/packages/dotlrn-ecommerce/lib/tree-chunk.tcl?rev=1.2&view=markup

11:48 AM, 14 Mar 2007 by Brian Fenton Permalink | Comments (0)

XML

Archive

March 2007
S M T W T F S
       
10 
11  12  13  14  15  16  17 
18  19  20  21  22  23  24 
25  26  27  28  29  30  31 
April 2007
March 2007
October 2006
March 2006
February 2006
January 2006
December 2005
November 2005
August 2005
July 2005
June 2005
May 2005

Notifications

You may request notification for OpenACS Blog.

Syndication Feed

XML

Recent Comments

  1. Silvio Ernesto: hola
  2. Martin v. Löwis: Cannot run pg_ctl from launchd
  3. Joan Miller: Something Interesting
  4. Hubert Erlmoser: trustpharma.com
  5. Hubert Erlmoser: TRUSTPHARMA
  6. Ben Koot: Selva
  7. LARRY SCOVEL: TRUST PHARMA
  8. Nick Carroll: Theme Selva
  9. Ben Koot: More enhancements
  10. Jane Perez: Trustpharma.com