On Thursday, March 3, 2011 10:31:18 AM UTC-5, Ross Peoples wrote:
>
> I know that web2py has HTML helpers like TABLE(), TR(), etc, but how would
> I build a table in this way, while in a for loop? This is what I tried so
> far, but it fails so horribly I don't even know where the error is:
>
> {{extend 'layout.html'}}
> <h1>Time Clock for {{=user.first_name}} {{=user.last_name}}</h1>
> {{if len(rows) > 0:}}
> {{
> def get_day_of_year(the_date):
> return the_date.timetuple().tm_yday
>
> def full_date_name(the_date):
> return the_date.strftime('%A, %B %d, %Y')
>
> def time_string(the_date):
> return the_date.strftime('%I:%M %p')
>
> prev_day_of_year = None
> tables = []
> trs = None
> for row in rows:
> if get_day_of_year(row.calculated_start) != prev_day_of_year:
> if trs is not None:
> tables[] = (H4(full_date_name(row.calculated_start)),
> trs)
> trs = []
>
> trs[] = TR((TD(time_string(row.calculated_start)),
> TD(time_string(row.calculated_end))))
> prev_day_of_year = get_day_of_year(row.calculated_start)
> else:
> trs[] = TR((TD(time_string(row.calculated_start)),
> TD(time_string(row.calculated_end))))
>
Am I missing something here? Where are you building a table -- I don't see
any calls to TABLE(), just TR(). Also, I'm not sure about your syntax. It
looks like you are passing a tuple of TD's to TR instead of passing the TD's
as separate arguments (i.e., there's an extra set of parentheses inside TR).
Finally, what is 'trs[] =' doing -- I think that will raise a syntax error.
You should be able to create a table object (e.g., table = TABLE()), and
then append TR's to it via append -- e.g., table.append(TR('cell 1', 'cell
2')).
Anthony