XForms/Selecting from Model

From Wikibooks, open books for an open world
Jump to: navigation, search

Motivation[edit]

You want to store lists in the model, not in the user interface. This allows a list to be used many places in a form and also enables lists to be read from external files (see the next example).

Screen Image[edit]

XForms-select-from-model.jpg

Execute Sample XForms Application[edit]

Load XForms Application

Sample Program[edit]

This example shows how to use the XForms itemset element to get data from the selection list directly from the model. Note that itemset is very similar to the group command in syntax: the outer loop tells you where to get your data and the inner loop binds a data element to a form value or item.

<html xmlns="http://www.w3.org/1999/xhtml" 
   xmlns:xf="http://www.w3.org/2002/xforms">
    <head>
        <title>Getting Selection List Data From the XForms Model</title>
        <style type="text/css"><![CDATA[
            body {font-family: Helvetica, Verdanan, sans-serif;}
        ]]>
        </style>
        <xf:model>
 
            <!-- this instance holds the data you send to the server on save -->
            <xf:instance id="my-item" xmlns="">
                <data>
                    <!-- the default color is red -->
                    <ItemColorCode>red</ItemColorCode>
                </data>
            </xf:instance>
 
            <!-- this instance holds the code tables used for all selection lists -->
            <xf:instance id="code-tables" xmlns="">
                <code-tables>
                    <code-table>
                        <code-table-id>ItemColorCode</code-table-id>
                        <item>
                            <label>Red</label>
                            <value>red</value>
                        </item>
                        <item>
                            <label>Orange</label>
                            <value>orange</value>
                        </item>
                        <item>
                            <label>Yellow</label>
                            <value>yellow</value>
                        </item>
                        <item>
                            <label>Green</label>
                            <value>green</value>
                        </item>
                        <item>
                            <label>Blue</label>
                            <value>blue</value>
                        </item>
                    </code-table>
                </code-tables>
            </xf:instance>
        </xf:model>
    </head>
    <body>
        <h1>Getting Selection List Data From the XForms Model</h1>
        <xf:select ref="ItemColorCode" appearance="full">
            <xf:itemset
                nodeset="instance('code-tables')/code-table[code-table-id='ItemColorCode']/item">
                <xf:label ref="label"/>
                <xf:value ref="value"/>
            </xf:itemset>
        </xf:select>
        <br/> Output: <xf:output ref="ItemColorCode"/>
    </body>
</html>

Discussion[edit]

This is a much more flexible way to manage lists. The instance document may be fetched dynamically from a remote database and the form does not need to be updated when lists change.


Next Page: Selecting from File | Previous Page: Select1 Multi-Column

Home: XForms