Matt Wilson wrote:
> I have a database table that will be empty now and later will have rows
> in it.
>
> Once I have rows, I want to create a SingleSelectWidget that allows
> users to choose rows.
>
> In the mean time, if the database table is empty, I don't want to
> display the select form in my template.
>
> I wrote this goofy function that either returns a select widget, or
> None, depending on if the underlying table is empty:

I see two options here, one relatively easy, one slightly harder.

The easy one is to make an empty widget that returns nothing when its
display method is called, return that instead of None, and clean up
your template to just a ${carriers.display()} call.

The harder one would be to alter SingleSelectField so that it does not
emit html if there are no items in the list.

-Adam
>
> def mk_select(t, c, widgetname):
>     """
>     Return None or a select widget that uses table ID for value.
>     t is table.
>     c is column to be used for the label.
>     """
>
>     rows = t.select()
>     if rows.count() > 0:
>         s = widgets.SingleSelectField(widgetname,
>                                       options=[(x.id, getattr(x, c))
> for x in rows])
>
>     else: s = None
>
>     return s
>
> carriers=mk_select(model.Carrier, 'carriername', 'carrier')
>
> Then in my template, I do this:
>
> <tr
> py:if="carriers"><td>Carrier</td><td>${carriers.display()}</td></tr>
>
> This strikes me as really ugly.
> 
> What am I doing wrong?
> 
> Matt


--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to