Table support
Rendering tables using Albatross Forms is relatively straightforward, using them for input is no harder.
Table support revolves around two classes: IteratorTable and IteratorTableRow.
IteratorTable acts as a field in a form in which the table is rendered.
IteratorTableRow should be subclassed within the application to render each row in turn.
Here's an example which should render a list of name, address and phone numbers in a table. First we define the model object:
Now we'll define the components of the form to render a list of Entry instances.
1 class EntryTableRow(IteratorTableRow):
2 def __init__(self, entry):
3 cols = (
4 Col((TextField('Name', 'name'), )),
5 Col((TextField('Address', 'address'), )),
6 Col((TextField('Phone', 'phone'), )),
7 )
8 IteratorTableRow.__init__(self, cols)
9
10 self.load(invoice_header)
11
12
13 class EntryTableForm(Form):
14 def __init__(self, entries):
15 headers = Row((
16 HeaderCol((Label('Name'), )),
17 HeaderCol((Label('Address'), )),
18 HeaderCol((Label('Phone'), )),
19 ))
20 self.table = IteratorTable('table', headers, EntryTableRow, entries,
21 html_attrs={'width': '100%'})
22 Form.__init__(self, 'Address book', (self.table, ))
To create the form:
1 def page_enter(ctx):
2 entries = ((
3 Entry('Ben Golding', 'Object Craft, 123/100 Elizabeth St, Melbourne Vic 3000', '+61 3 9654-9099'),
4 Entry('Dave Cole', 'Object Craft, 123/100 Elizabeth St, Melbourne Vic 3000', '+61 3 9654-9099'),
5 ))
6 if not ctx.has_value('entry_table_form'):
7 ctx.locals.entry_table_form = EntryTableForm(entries)
8 ctx.add_session_vars('entry_table_form')
Rendering the list just requires:
<al-form method="post"> <div class="alxform"> <alx-form name="entry_table_form" static /> </div> </al-form>
To support editing the fields, you would change how it renders using:
<al-form method="post"> <div class="alxform"> <alx-form name="entry_table_form" errors /> </div> </al-form>
When the user has made changes, your page_process method can pick up the changes using: