跳转到内容

XForms/选择和分组

来自维基教科书,自由的教学读物

您希望根据从列表中选择的值有条件地显示一组元素。这将与“switch/case”类似,但您可以使每个视图依赖于复杂的 XPath 表达式,这些表达式将被评估为真或假。

屏幕图像

[编辑 | 编辑源代码]
使用 bind 和 select1 有条件地显示组

在上面的屏幕图像中,选择了第二个项目。当选择不同的项目时,选择列表下的视图会发生变化。

示例程序

[编辑 | 编辑源代码]
<html
   xmlns="http://www.w3.org/1999/xhtml"
   xmlns:xf="http://www.w3.org/2002/xforms">
   <head>
      <title>Dynamically bind to a group</title>
      <!-- Using bind and relevant in the model to conditionally display a group -->
      <!-- Alternative to switch/case/toggle when the id of toggle is dynamically calculated -->
      <style type="text/css">
      @namespace xf url("http://www.w3.org/2002/xforms");
      xf|group {
         border: solid black 2px;
         background-color: silver;
         height: 100px;
      }
      xf|group xf|label {
         position: relative;
         font-family: Helvetica, sans-serif;
         font-weight: bold;
         background-color: white;
         padding: 2px;
         top: -10px;
         left: 10px;
      }
      xf|group p {
         position: relative;
         top: -30px;
         padding: 5px;
      }
      </style>
      <xf:model>
         <xf:instance>
            <data xmlns="">
               <current-view>one</current-view>
               <view-1>one</view-1>
               <view-2>two</view-2>
               <view-3>three</view-3>
            </data>
         </xf:instance>
         <!-- if the current-view is 'one' make the view-1 group relevent (visible)-->
         <xf:bind nodeset="view-1" relevant="../current-view = 'one'" />
         <xf:bind nodeset="view-2" relevant="../current-view = 'two'" />
         <xf:bind nodeset="view-3" relevant="../current-view = 'three'" />
      </xf:model>
   </head>
   <body>
         <xf:select1 ref="current-view">
            <xf:label>Select View: </xf:label>
            <xf:item>
               <xf:label>One</xf:label>
               <xf:value>one</xf:value>
            </xf:item>
            <xf:item>
               <xf:label>Two</xf:label>
               <xf:value>two</xf:value>
            </xf:item>
            <xf:item>
               <xf:label>Three</xf:label>
               <xf:value>three</xf:value>
            </xf:item>
         </xf:select1>
         <br/>
         <!-- only one of the three outputs will display -->
         <xf:output ref="view-1">
            <xf:label>Current view: </xf:label>
         </xf:output>
         <xf:output ref="view-2">
            <xf:label>Current view: </xf:label>
         </xf:output>
         <xf:output ref="view-3">
            <xf:label>Current view: </xf:label>
         </xf:output>
         <br/>
         <br/>
         <!-- only a single group will be displayed at any time -->
         <xf:group ref="view-1">
            <xf:label>View One</xf:label>
            <p>One One One One One One One One One One One One One One One One One One</p>
         </xf:group>
         <xf:group ref="view-2">
            <xf:label>View Two</xf:label>
            <p>Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two</p>
         </xf:group>
         <xf:group ref="view-3">
            <xf:label>View Three</xf:label>
            <p>Three Three Three Three Three Three Three Three Three Three Three Three Three Three Three Three Three</p>
         </xf:group>
   </body>
</html>

此程序与使用按钮切换 switch/case 的示例非常相似,但该值是通过任何 XPath 表达式动态计算的。使用 switch/case/toggle 的先前示例使用 XML 事件来使特定情况可见。

参考文献

[编辑 | 编辑源代码]
下一页: 动态标签 | 上一页: 只读
首页: XForms
华夏公益教科书