Kaya 0.5.2 reference for HTMLDocument::addSelectElement

List of functions and data types in HTMLDocument

ElementTree addSelectElement(ElementTree block, String name, Int ssize, [Pair<String, [SelectOption]> ] optgroups)

Arguments

Usage

Adds a selection box to a form. The optgroups parameter is a list of pairs. The first element of the pair is the 'heading' for the option group, and the second element is a list of options in that group. For most simple selectors, a single option group with no heading (the empty string) is sufficient.

options = [
    SelectOption("Express delivery","1",true),
    SelectOption("Standard delivery","2",false),
    SelectOption("Slow delivery","3",false)
];
sel = addSelectElement(fieldset,"choice",0,[("",options)]);
/* // produces 
<select>
  <option value='1' selected='selected'>Express delivery</option>
  <option value='2'>Standard delivery</option>
  <option value='3'>Slow delivery</option>
</select>
*/

Using multiple groups of options is useful for larger select elements, where it can make the form clearer.

singles = ["A1","A2","B5"];
twins = ["A4","C2"];
doubles = ["A7","C1","C3"];
sopts = [];
topts = [];
dopts = [];
for s in singles {
    push(sopts,SelectOption(s,s,false);
}
for t in twins {
    push(topts,SelectOption(s,s,false);
}
for d in doubles {
    push(dopts,SelectOption(s,s,false);
}
options = [
    ("Single rooms",sopts),
    ("Twin rooms",topts),
    ("Double rooms",dopts)
];
sel = addSelectElement(roombooker,"room",0,options);

Select elements allowing multiple options to be selected have very bad usability in most browsers - it is often better to use addOptionList to generate a set of checkboxes instead. Whichever method you use for multiple selection, remember that you need to use WebCommon::incomingData to correctly retrieve the selections from the user's form submission.

Related

kaya@kayalang.org | Last modified 29 November 2011 | Supported by Durham CompSoc | Powered by Kaya