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

Changes between Version 4 and Version 5 of SelectingOption


Ignore:
Timestamp:
12/03/05 03:06:25 (14 years ago)
Author:
nic at bellamy dot co dot nz
Comment:

Added dynamic select box example

Legend:

Unmodified
Added
Removed
Modified
  • SelectingOption

    v4 v5  
    1 = Selecting a HTML option = 
     1= Selecting an HTML option = 
    22 
    33From the mailing list ... 
     
    3030</select> 
    3131}}} 
     32 
     33== Dynamic select boxes == 
     34 
     35This can be easily extended to dynamic select boxes, for example: 
     36 
     37{{{ 
     38#!python 
     39from model import YourDataType 
     40 
     41class YourController: 
     42    @turbogears.expose(html='yourtemplate') 
     43    def index(self): 
     44        items = YourDataType.select() 
     45        # item_id is used to added the "selected" attribute to the correct <option> 
     46        # Below is just for example: in reality, this value would be pulled from 
     47        # another SQLObject (or other source) 
     48        item_id = 3 
     49        return dict(items = items, item_id = item_id) 
     50}}} 
     51 
     52Then in the template: 
     53 
     54{{{ 
     55#!text/html 
     56<select name="the_item"> 
     57   <option py:for="i in items" value="${i.id}" py:attrs="selected=std.selector(i.id == item_id)">${i.name}</option> 
     58</select> 
     59}}}