Forum OpenACS Q&A: select option lists

Collapse
Posted by Jade Rubick on
One of the most tedious parts of developing in ACS 3.4x is dealing with option list default values. Example:
if {$value == "1"} {
  set sel ""
} elseif {$value == "2"} {
  set sel "selected"
}
append page_body "<select name=option_list>
<option value="1">One</option>
<option $sel value="2">Two</option>
</select>
Is there a better way to do this? When you have multiple items in your select lists, setting the default value can be quite tedious.

A few Tcl based improvements come to mind, but I was wondering if there was some widget or function that I didn't know about that helped here.

Collapse
Posted by James Thornton on
yes, look in ad-utilities.tcl.preload...
  • html_select_options
  • db_html_select_options
  • html_select_value_options
  • db_html_select_value_options
Collapse
Posted by David Walker on
You could try:

append page_body "<select name=option_list>"

set mylist [list 1 One 2 Two 3 Three]
foreach {optionval optionname} $mylist {
 append page_body "<option value="$optionval" "
 if {[string equal $value $optionval]} {
  append page_body " selected "
 }
 append page_body " > $optionname </option>
"
}

append page_body "</select>"
Collapse
Posted by Tom Jackson on

Not quite related with this issue, but just as annoying is having a database field that can only have a few values. Usually this is supported with a check constraint. Recently, I decided to move all check constraint fields into foreign key constraints and have a small helper table. The table holds the small integer primary key, a short description of the value and usually a boolean indicating that the entry is useable/displayable.

Then in the ACS templating system, I select from this table to create a multirow datasource. On the adp template page, I use code like this (sorry if this doesn't make through the email):

<multiple name=mychoices> <option value="@mychoices.choice_id@" <if @curval@ eq @mychoices.choice_id@>selected</if>>@mychoices.description@</option> </multiple>

If you need a default set even for new insert selects, I guess a separate column indicating a default would help.