dwr.util.addOptions()

addOptions allows you to add elements to ol, ul or select elements. For worked examples and see the list manipulation documentation.

Syntax:

dwr.util.addOptions(...);

Description:

dwr.util.addOptions has 5 modes with different options passed in. For each a number of options or list items are created. For <ul> and <ol> lists this will be an <li> element, with customized contents. For <select> lists, this will be an <option> element with customized text (that's shown to the user) and value (that's sent with a form submission).

The modes are:

Array of Strings

For example:

dwr.util.addOptions(id, [ "one", "two", "three" ]);

When id is a select element, this will create a set of options with the text and value set to the string version of each array element.

When id is an ol or ul element, this will create a set of li elements with their contents set to the string version of each array element.

Array of Objects

For example:

var data = [ { name:"Fred" }, { name:"Jim" } ];
dwr.util.addOptions(id, data, "name");

When id is a select element, this creates an option for each array element, with the value and text of the option set to the given property of each object in the array. In this case "Fred" and "Jim".

When is is a ul or ol list, a set of li elements are created with their contents set to these strings.

Array of Objects (select lists only)

Syntax:

dwr.util.addOptions(selectid, data, valueprop, textprop);

This version creates an option for each object in the array, with the value of the option set to the given valueprop property of the object, and the option text set to the textprop property.

In addition to passing in the strings for valueprop and textprop, you can also pass in functions which will be called with the object as a parameter, and expected to return data for the option value/text.

This does not work with ol or ul lists.

Array (ol and ul lists only)

Syntax:

function formatter(entry) { return "data=" + entry; }
dwr.util.addOptions(id, data, formatter, [options]);

This allows you to format objects before placing them into a ol or ul list. By default DWR escapes the HTML to protect from XSS attacks, however this action can be changed using an options object: { escapeHtml:false }. This action is similar to that employed by addRows and setValue.

Object

Syntax:

dwr.util.addOptions(selectid, map, reverse);

This creates an option for each property. The property names are used as option values, and the property values are used as option text, which sounds wrong, but is actually normally the right way around. If reverse evaluates to true then the property values are used as option values.

Map of Objects

Syntax:

dwr.util.addOptions(selectid, map, valueprop, textprop);

This mode of operation is deprecated and may be removed in the future.

Creates an option for each object in the map, with the value of the option set to the given valueprop property of the object, and the option text set to the textprop property.