Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Ticket #475 (closed enhancement: fixed)

Opened 13 years ago

Last modified 13 years ago

DynamicSelectField widget

Reported by: spleeman@… Owned by: anonymous
Priority: normal Milestone: 0.9
Component: TG Widgets Version:
Severity: normal Keywords:
Cc:

Description

Sometimes you don't know what a SelectField?'s options are going to be before you .insert() it and changing the options on the fly isn't threadsafe.

Attached is a patch with a new widget called DynamicSelectField?. It's essentially the same as a normal SelectField? except it pulls out it's options from widget_value on .insert().

Examples:

<div>
    ${testwidget.insert(value=(3, [(1,"Apple"),(2,"Banana"),(3,"Pear")]))}
</div>
<div>
    ${testwidget.insert(value=(2, [(1,"Audi"),(2,"VW"),(3,"Ford")]))}
</div>

The first argument in the value tuple is the value of the option you want to be selected and the second is a list of tuples containing the options and their respective values and text.

With this method the data can still be passed when the widget is nested in a Form Widget (or subclass thereof).

Attachments

DynamicSelectField.patch Download (1.2 KB) - added by spleeman@… 13 years ago.
DynamicSelectField? patch

Change History

Changed 13 years ago by spleeman@…

comment:1 Changed 13 years ago by kevin

David Bernard and Michele Cella are looking more closely at the widget APIs and cleaning things up a bit. Something like this is needed, but I'm not sure that it's a new widget. (It may just be an enhanced version of the existing widget.)

comment:2 Changed 13 years ago by kevin

  • Summary changed from [PATCH] DynamicSelectField widget to DynamicSelectField widget

comment:3 Changed 13 years ago by Dan Jacob

In the meantime, you can pass a callable as the options argument, which is evaluated when the widget is rendered, for example:

def get_clients():

return [(c.id, c.name) for c in model.Client.select()]

SelectField?("clientID", options=get_clients, validator=validators.Int())

comment:4 Changed 13 years ago by alberto

  • Status changed from new to closed
  • Resolution set to fixed
  • Component changed from CherryPy to Widgets

The new API already allows passing options at display time:

${widget.display(options=[(1, "one"), (2, "two"), (3, "three")])}
Note: See TracTickets for help on using tickets.