XForms/從模型中選擇
外觀
< XForms
您希望將列表儲存在模型中,而不是使用者介面中。這允許列表在表單中的許多地方使用,並使列表能夠從外部檔案讀取(請參閱下一個示例)。

此示例演示如何使用 XForms 的 itemset 元素直接從模型中獲取選擇列表中的資料。請注意,itemset 在語法上與 group 命令非常相似:外迴圈告訴您從哪裡獲取資料,內迴圈將資料元素繫結到表單值或專案。
<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>
這是一種管理列表更靈活的方法。例項文件可以從遠端資料庫動態獲取,當列表更改時,表單不需要更新。