跳转到内容

XForms/实体选择

来自维基教科书,开放的书籍,开放的世界

您需要一个用户界面控件,允许用户在合同中选择一个合法实体,例如个人或组织。 人员或组织的列表将在一个包含 ID 和屏幕标签的列表中提供。 您需要即时输入功能来缩小来自这些列表的选择范围。

我们将创建一个包含两个标签的用户界面控件。 第一个标签用于人员,第二个标签用于组织。

屏幕图像

[编辑 | 编辑源代码]
实体选择器控件。 请注意,只显示包含字符串“John”的人名

源代码

[编辑 | 编辑源代码]
<html xmlns="http://www.w3.org/1999/xhtml" 
   xmlns:ev="http://www.w3.org/2001/xml-events" 
   xmlns:xf="http://www.w3.org/2002/xforms">
    <head>
        <title>Entity Selector</title>
        <link rel="stylesheet" type="text/css" href="entity-selector.css"/>
        <xf:model>
            <xf:instance xmlns="" id="save-data">
                <data>
                    <Person>
                        <PersonID></PersonID>
                        <PersonLabel></PersonLabel>
                    </Person>
                       <Organization>
                          <OrganizationID></OrganizationID>
                          <OrganizationLabel></OrganizationLabel>
                       </Organization>
                </data>
            </xf:instance>
            
            <xf:instance xmlns="" id="views">
                <data>
                    <persons></persons>
                    <organizations></organizations>
                </data>
            </xf:instance>
            
            <xf:instance xmlns="" id="candidate-people">
                <data>
                    <Person>
                        <PersonID>1</PersonID>
                        <PersonLabel>Amy Smith</PersonLabel>
                    </Person>
                    <Person>
                        <PersonID>2</PersonID>
                        <PersonLabel>Becky Adams</PersonLabel>
                    </Person>
                    <Person>
                        <PersonID>3</PersonID>
                        <PersonLabel>Mark Daniels</PersonLabel>
                    </Person>
                    <Person>
                        <PersonID>4</PersonID>
                        <PersonLabel>Dan Jones</PersonLabel>
                    </Person>
                    <Person>
                        <PersonID>5</PersonID>
                        <PersonLabel>Fred Johnson</PersonLabel>
                    </Person>
                    <Person>
                        <PersonID>6</PersonID>
                        <PersonLabel>Sue Johnson</PersonLabel>
                    </Person>
                    <Person>
                        <PersonID>7</PersonID>
                        <PersonLabel>John Doe</PersonLabel>
                    </Person>
                    <Person>
                        <PersonID>8</PersonID>
                        <PersonLabel>Jane Smith</PersonLabel>
                    </Person>
                    <Person>
                        <PersonID>9</PersonID>
                        <PersonLabel>Jake Anderson</PersonLabel>
                    </Person>
                    <Person>
                        <PersonID>10</PersonID>
                        <PersonLabel>Jason Burke</PersonLabel>
                    </Person>
                    <Person>
                        <PersonID>11</PersonID>
                        <PersonLabel>Jerome Hughes</PersonLabel>
                    </Person>
                    <Person>
                        <PersonID>12</PersonID>
                        <PersonLabel>Robert Paulson</PersonLabel>
                    </Person>
                    <Person>
                        <PersonID>13</PersonID>
                        <PersonLabel>Paul Robertson</PersonLabel>
                    </Person>
                </data>
            </xf:instance>
            
            <xf:instance xmlns="" id="candidate-organizations">
                <data>
                    <Organization>
                        <OrganizationID>1</OrganizationID>
                        <OrganizationLabel>Courts</OrganizationLabel>
                    </Organization>
                    <Organization>
                        <OrganizationID>2</OrganizationID>
                        <OrganizationLabel>County</OrganizationLabel>
                    </Organization>
                    <Organization>
                        <OrganizationID>3</OrganizationID>
                        <OrganizationLabel>Prosecution</OrganizationLabel>
                    </Organization>
                    <Organization>
                        <OrganizationID>4</OrganizationID>
                        <OrganizationLabel>Probation</OrganizationLabel>
                    </Organization>
                    <Organization>
                        <OrganizationID>5</OrganizationID>
                        <OrganizationLabel>Public Defender</OrganizationLabel>
                    </Organization>
                    <Organization>
                        <OrganizationID>6</OrganizationID>
                        <OrganizationLabel>Jails</OrganizationLabel>
                    </Organization>
                    <Organization>
                        <OrganizationID>7</OrganizationID>
                        <OrganizationLabel>Prison</OrganizationLabel>
                    </Organization>
                    <Organization>
                        <OrganizationID>8</OrganizationID>
                        <OrganizationLabel>State</OrganizationLabel>
                    </Organization>
                </data>
            </xf:instance>
            
            <xf:action ev:event="xforms-ready">
                <xf:setfocus control="person-input"/>
            </xf:action>
            
            <xf:submission id="save" 
                action="echo.xq" method="post"/>
        </xf:model>
    </head>
    <body>
        <h1>Entity Selector</h1>
        <p>Select either a person or an organization:</p>
        <div class="horiz-tabs-menu">
            <div id="tab1">
                <a href="#tab1">Person
                    <xf:toggle case="case-1" ev:event="DOMActivate"/>
                </a>
            </div>
            <div id="tab2">
                <a href="#tab2">Organization
                    <xf:toggle case="case-2" ev:event="DOMActivate"/>
                </a>
            </div>
        </div>
        <xf:switch>
            <xf:case id="case-1" selected="true()">                
                <xf:input ref="Person/PersonLabel" incremental="true" class="PersonLabel" id="person-input">
                    <xf:label>Person: </xf:label>
                </xf:input>
                <br/>
                <div class="autoscroll">
                    <!-- list all the candidate people and filter out the ones that start with the prefix -->
                    <xf:repeat 
                        nodeset="instance('candidate-people')/Person[contains(PersonLabel, instance('save-data')/Person/PersonLabel)]"
                    id="persons-repeat">
                    <xf:trigger appearance="minimal">
                        <xf:output ref="PersonLabel"></xf:output> (<xf:output ref="PersonID"></xf:output>)
                        <!-- now copy the selected item to the save instance -->
                        <xf:action ev:event="DOMActivate">
                            <!-- this should work <xf:insert
                                origin="instance('candidate-people')/Person[index('persons-repeat')]"                        
                                nodeset="instance('save-data')/Person"/>
                                -->
                            
                            <xf:setvalue
                                ref="instance('save-data')/Person/PersonLabel" 
                                value="instance('candidate-people')/Person[contains(PersonLabel, instance('save-data')/Person/PersonLabel)][index('persons-repeat')]/PersonLabel" />
                            <xf:setvalue
                                ref="instance('save-data')/Person/PersonID" 
                                value="instance('candidate-people')/Person[contains(PersonLabel, instance('save-data')/Person/PersonLabel)][index('persons-repeat')]/PersonID" /> 
                        </xf:action>
                    </xf:trigger>
                </xf:repeat>
                </div>
            </xf:case>
            <xf:case id="case-2">
                <xf:input ref="Organization/OrganizationLabel" incremental="true">
                    <xf:label>Organization: </xf:label>
                </xf:input>
                <div class="autoscroll">
                    <!-- list all the candidate people and filter out the ones that start with the prefix -->
                    <xf:repeat
                        nodeset="instance('candidate-organizations')/Organization[contains(OrganizationLabel, instance('save-data')/Organization/OrganizationLabel)]" 
                        id="organizations-repeat">
                        <xf:trigger appearance="minimal">
                            <xf:output ref="OrganizationLabel"></xf:output>
                            (<xf:output ref="OrganizationID"></xf:output>)
                            <!-- now copy the selected item to the save instance -->
                            <xf:action ev:event="DOMActivate">
                                <xf:setvalue
                                    ref="instance('save-data')/Organization/OrganizationLabel" 
                                    value="instance('candidate-organizations')/Organization[contains(OrganizationLabel, 
                                    instance('save-data')/Organization/OrganizationLabel)][index('organizations-repeat')]/OrganizationLabel" /> 
                                <xf:setvalue
                                    ref="instance('save-data')/Organization/OrganizationID" 
                                    value="instance('candidate-organizations')/Organization[contains(OrganizationID, 
                                    instance('save-data')/Organization/OrganizationID)][index('organizations-repeat')]/OrganizationID" /> 
                            </xf:action>
                        </xf:trigger>
                    </xf:repeat>
                </div>
            </xf:case>
        </xf:switch>
        <br/><br/>
        <div class="test">
            <xf:output ref="instance('save-data')/Person/PersonLabel">
                <xf:label>Person:</xf:label>
            </xf:output>
            (<xf:output ref="instance('save-data')/Person/PersonID"/>)
            <br/>
            <xf:output ref="Organization/OrganizationLabel">
                <xf:label>Organization:</xf:label>
            </xf:output>
            (<xf:output ref="instance('save-data')/Organization/OrganizationID"/>)
        </div>
        <xf:submit submission="save">
            <xf:label>Save</xf:label>
        </xf:submit>
    </body>
</html>
下一页: 重复到表格 | 上一页: 三文档加载
首页: XForms
华夏公益教科书