XForms/條件操作
外觀
< XForms
您希望根據 XPath 表示式有條件地執行操作。
我們將使用 XForms 1.1 規範中作為操作一部分的 if 屬性。我們將設定一個事件,該事件將在例項變為空時觸發。我們將建立一個操作並將 observer 屬性設定為監視 people 例項中的更改。
以下是操作本身的程式碼
<xf:action
ev:event="xforms-delete"
ev:observer="people"
if="not(person)">
<xf:insert origin="instance('person-template')" context="."/>
</xf:action>
這表示監視 people 例項,如果 people 例項中沒有人員,則使用 person-template 例項插入一個人。
<html
xmlns="http://www.w3.org/1999/xhtml"
xmlns:xf="http://www.w3.org/2002/xforms"
xmlns:xsd="http://www.w3.org/2001/XMLSchema-datatypes"
xmlns:ev="http://www.w3.org/2001/xml-events">
<head>
<title>Test of populating a repeat if it becomes empty</title>
<xf:model id="m">
<xf:instance id="people">
<people xmlns="">
<person>
<name>John</name>
<email>j...@example.org</email>
</person>
<person>
<name>Bethany</name>
<email>beth...@example.org</email>
</person>
</people>
</xf:instance>
<xf:instance id="person-template">
<person xmlns=""><name/> <email/></person>
</xf:instance>
</xf:model>
</head>
<body>
<h1>Test of populating a repeat if it becomes empty</h1>
<xf:group ref="instance('people')">
<xf:repeat nodeset="person">
<xf:input ref="name"><xf:label>Name: </xf:label></xf:input><br/>
<xf:input ref="email"><xf:label>Email address: </xf:label></xf:input>
<xf:trigger>
<xf:label>Delete</xf:label>
<xf:delete ev:event="DOMActivate" nodeset="."/>
</xf:trigger>
</xf:repeat>
<xf:action ev:event="xforms-delete" ev:observer="people"
if="not(person)">
<xf:insert origin="instance('person-template')" context="."/>
</xf:action>
</xf:group>
</body>
</html>
此示例由 John L. Clark 於 2008 年 12 月 10 日釋出在 Mozilla XForms 新聞組上。