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 1 and Version 2 of CheckedCheckBox


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

Reformatted; added dynamic example

Legend:

Unmodified
Added
Removed
Modified
  • CheckedCheckBox

    v1 v2  
     1= Automatically checking a checkbox or radio button = 
     2 
     3The following: 
     4 
     5{{{ 
     6#!text/html 
    17<input type="checkbox" py:attrs="checked=std.checker(expression)" /> 
     8}}} 
    29 
    3 will create a checked checkbox if the expression is true 
     10will create a checked checkbox if the expression is true. 
     11 
     12== Dynamic checkboxes == 
     13 
     14To create a dynamic list of checkboxes (or radio buttons), with the appropriate ones pre-checked: 
     15 
     16{{{ 
     17#!python 
     18from model import YourDataType 
     19 
     20class YourController: 
     21    @turbogears.expose(...) 
     22    def index(self): 
     23        items = YourDataType.select() 
     24        # Select some examples items; in reality, these would be pulled from another SQLObject or other source 
     25        items_selected = [4, 5, 9] 
     26        return dict(items = items, items_selected = items_selected) 
     27}}} 
     28 
     29Then in the template: 
     30 
     31{{{ 
     32#!text/html 
     33<!-- Using py:strip below means that the div will not be in the resulting HTML; we're just using it as a wrapper --> 
     34<div py:for="i in items" py:strip="True"> 
     35  <label><input type="checkbox" value="${i.id}" py:attrs="checked=std.checker(i.id in items_selected)"/>${i.label}</label> 
     36</div> 
     37}}}