Skip to content

How to create a combo box using the select tag function

macourtney edited this page Sep 13, 2010 · 4 revisions

Added for version: 0.4
Updated for version: 0.6

To create a combobox, use the select-tag function. Select-tag takes a map which will contain an options key mapped to a sequence of possible options. The options may be simply keywords, string, or maps describing the individual option.

To create a select tag with just the options red, yellow, and green, use:

(select-tag { :options [ :red, :yellow, :green ] })

If you want to set the values to something other than the displayed text you can pass maps for each option. The value of :value will be the value of the option, and :name will be the displayed valued of the option.

(select-tag { 
  :options 
    [ { :name "Red", :value :red }, 
      { :name "Yellow", :value :yellow },
      { :name "Green", :value :green } ] })

You can set the selected option by setting :select to true in the map for the option you wish to select:

(select-tag { 
  :options 
    [ { :name :red }, 
      { :name :yellow, :select true },
      { :name :green } ] })

Note: In the above example, since :value is not set, :name is used as the value of each option.

Putting it all together with a record: If you have a record called :frog with value “green” under the key :color. Use the following to create a select-tag for it:

(select-tag { :color "green" } :frog :color 
  { :options 
    [ { :name "Red", :value :red }, 
      { :name "Yellow", :value :yellow },
      { :name "Green", :value :green } ] })

Conjure 0.4 Combo box

Comboboxes changed in Conjure 0.6. To create a combobox in the older 0.4 version of Conjure use this section.

Select-tag takes a record, the name of the record, the key of the record to get the value from, and select-options. You can also just pass select-options if the select-tag is unrelated to a record.

To create a select-tag with the options “red”, “yellow”, and “green” use:

(select-tag { :option-map { :red nil, :yellow nil, :green nil } })

If you want to use the the select tag with a record, and have a value in the record used as the value of the select you can pass the record, the name of the record and key of the record to get the value from.

If you have a record called :frog with value “green” under the key :color. Use the following to create a select-tag for it:

(select-tag { :color "green" } :frog :color { :option-map { :red nil, :yellow nil, :green nil } })

If you want to set the values to something other than the displayed text you can pass maps as the values of the options.

(select-tag { :color "green" } :frog :color 
  { :option-map { :Red { :value "red" }, :Yellow { :value "yellow" }, :Green { :value "green" } } })

In the above call to select-tag, “Red” is displayed as the text of the option, while “red” is used as the value passed back to the server.

You can use :html-options in select-options to set other attributes of the select-tag. To allow the user to select multiple options use:

(select-tag { :color "green" } :frog :color 
  { :option-map { :red nil, :yellow nil, :green nil }
    :html-options { :multiple "multiple" } })
Clone this wiki locally