dwr.util.setValues(object/array, options)
Similar to setValue except that the input can be either a Javascript object or an array of Javascript objects. The objects should contain name/value pairs. The names are assumed to be the IDs of HTML elements, and the values, what we should set the contents of the elements.
By default DWR protects against XSS in dwr.util.setValues by performing output escaping. The optional options object parameter allows output escaping to be disabled. Please see the escapeHtml page for more information.
- dwr.util.setValues(object) - An example of using setValues to populate a form with object values.
- dwr.util.setValues(array, { idPrefix : 'yourPrefixHere' } - An example of populating several div/spans with an array of object values.
- dwr.util.setValues(array, { idPrefix : 'yourPrefixHere' } - Similar to the second example except we are using dwr.util.cloneNodeForValues to dynamically generate the HTML elements for each element in the array of objects.
dwr.util.setValues(object) - Populate a form with object values
In this example we are using dwr.util.setValues to populate a form with data from an object. Please modify the object definition in the textarea and click the setValues button to experiment.
dwr.util.setValues(
)
Results:
HTML Test Elements
| Text area (id="textarea"): | Selection list (id="select"): | ||
| Text input (id="text"): | Password input (id="password"): | ||
| Form button (id="formbutton"): | Fancy button (id="button"): |
dwr.util.setValues(array, { idPrefix : 'yourPrefixHere' }
In this example we are using dwr.util.setValues to populate a series of divs/spans with data from an array of objects. Please modify the object definition in the textarea and click the setValues button to experiment.
var peopleArr =
dwr.util.setValues(peopleArr, { idPrefix : 'person' });
Results:
When an array is passed into setValues the id of the elements should be idPrefix[index].objectPropertyName. To illustrate this here is the HTML for the results above:
<div><span id="person[0].firstName"></span> <span id="person[0].lastName"></span></div>
<div><div>Address: <span id="person[0].address"></span></div></div>
<div><span id="person[1].firstName"></span> <span id="person[1].lastName"></span></div>
<div><div>Address: <span id="person[1].address"></span></div></div>
<div><span id="person[2].firstName"></span> <span id="person[2].lastName"></span></div>
<div><div>Address: <span id="person[2].address"></span></div></div>
dwr.util.setValues(array, { idPrefix : 'yourPrefixHere' } with dwr.util.cloneNodeForValues
This example is very similar to the previous example. The difference is that we are also using dwt.util.cloneNodeForValues so that we do not have to create all of the HTML elements.
var peopleArr =
dwr.util.cloneNodeForValues('personWithClone', peopleArr, {updateCloneStyle:{display:""}});(peopleArr, { idPrefix : 'personWithClone' });
dwr.util.setValues
Results:
The HTML below (all that is needed for this example) illustrates the work that dwr.util.cloneNodeForValues performs:
<div id="personWithClone" style="display:none" class="testblock">
<div>Name: <span id="personWithClone.firstName"></span> <span id="personWithClone.lastName"></span></div>
<div>Address: <span id="personWithClone.address"></span></div>
</div>