Next / Previous / Contents / TCC Help System / NM Tech homepage

6.7. addRadios(): Add regional selection radiobuttons

# - - -   a d d R a d i o s

def addRadios(form, db):
    '''Build the table of radiobuttons

      [ (form is a form et.Element) and
        (db is a pycbc.CBCDatabase instance) ->
          form  +:=  radiobuttons to select regions in db ]

We arrange the regional radiobuttons into columns using an ordinary XHTML table. It would be easier to spread the regions across the row first and then down the table, but this, in the author's opinion, is extremely crass. People expect to look across the tops of the columns to locate the correct column, then down the column.

If the number of regions is not an exact multiple of the number of columns, we also want the short column to be the last one. To compute the number of rows, we use Section 51, “divCeil(): Discrete rounding function”.

    #-- 1
    # [ regionList  :=  all the Region instances from db, in ascending
    #                   order by region name
    #   nRows  :=  number of rows necessary to arrange those instances
    #              into columns with the last column short ]
    regionList = [ reg
                   for reg in db.genRegions() ]
    nRows = lib.divCeil(len(regionList), N_REG_COLS)

Next we start the table. For the XHTML, see Section 4.1, “Layout of the top page”. See also Section 6.3.1, “N_REG_COLS.

    #-- 2
    # [ form  +:=  a new table element with N_REG_COLS columns
    #   table  :=  that table element ]
    table = subElement(form,
        E.table(cellpadding='8', border='0', frame='void'))
    for k in range(N_REG_COLS):

For the logic that builds each row of the table, see Section 6.8, “buildRow(): Build one row of region radiobuttons”.

    #-- 3
    # [ table  +:=  rows displaying regionList as nRows rows by
    #               N_REG_COLS columns ]
    for rowx in range(nRows):
        #-- 3 body
        # [ rowx is an index in regionList ->
        #     table  +:=  a row displaying a slice across regionList
        #         for row (rowx) displayed as nRows rows by
        #         N_REG_COLS columns ]
        buildRow(table, regionList, nRows, rowx)