跳轉到內容

XForms/從模型中選擇

來自華夏公益教科書,開放的書籍,開放的世界

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

螢幕影像

[編輯 | 編輯原始碼]

執行示例 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>

這是一種管理列表更靈活的方法。例項文件可以從遠端資料庫動態獲取,當列表更改時,表單不需要更新。


下一頁: 從檔案中選擇 | 上一頁: Select1 多列
主頁: XForms
華夏公益教科書