xpages:计算字段使用onchange事件

时间:2016-06-03 03:50:03

标签: xpages

我想问一下如何使用计算字段显示列表框值?

我的想法是有一个列表框和一个计算字段。在列表框中,我在onchange事件中使用部分更新,而部分更新ID是计算字段

我可以在这里发布关于listbbox和计算字段的代码:

对于列表框:

<xp:listBox id="listBox2">
    <xp:selectItems>
        <xp:this.value><![CDATA[#{javascript:viewScope.get("vs1");
}]]></xp:this.value>
    </xp:selectItems>
    <xp:eventHandler event="onchange" submit="true"
        refreshMode="partial" refreshId="computedField1">
    </xp:eventHandler></xp:listBox>

对于计算字段:在本部分中,我已阅读此帖:How to get display text of combobox and not the alias?以了解如何显示列表框值。

function getComponentLabel(componentId) {       
var select = getComponent(componentId); 
var value = select.getValue();
try {
    var list = select.getChildren();
    if (value) {
       for (var i = 0; i < list.length; i++) { 
          if ((typeof list[i]).indexOf("SelectItems") > -1) {
              items = list[i].getValue();
              for (var k = 0; k < items.length; k++) {
                  if (items[k].getValue() === value) { 
                     return items[k].getLabel();
                  }
              }
           }  
           else if((typeof list[i]).indexOf("SelectItem") > -1) {
             if (list[i].getItemValue() === value) { 
                 return list[i].getItemLabel();
             }
          }
       }   
   }
} catch (e) {       
}
return value;
}

var dspValue = getComponentLabel("listBox1");//yes, it's listbox 1 not listbox2, if type listbox2, the computed field will show null (not sure why) 

return "The listbox selected value(s) is: " + dspValue;

到目前为止,当我第一次运行代码时,我将值放在列表框中,计算字段可以显示列表框中的选定值。但是当我在列表框中放入多个值时,计算字段只能显示我已经放置的最新值。

我试图在计算字段中使用for循环来获取列表框中的选定值:

var text="";
for (var i = 0; i < dspValue.length; i++)
{
    text += dspValue[i];
}
return "you select value is: " + text;

然后我运行代码,我得到错误消息说dspValue为null。我也尝试在没有for循环的情况下显示dspValue.length并得到相同的错误。

我没有想要从列表框中获取所有值并显示在计算字段中,而不是使用for循环。

如何使用计算字段显示列表框中的所有选定值?我很感激您的任何建议。

2 个答案:

答案 0 :(得分:0)

您可能需要组件的submittedValue,请参阅问题中的Paul Withers答案,xPage dateTime picker validation does not work on date change

答案 1 :(得分:0)

一种可能的解决方案是简化它,而不是使用计算字段,使用重复的只读&#39;而是列表框。这样,列表框的只读渲染器将完成确定适当标签的繁重工作。您只需要确保两个列表框具有相同的选择值。

<!-- This is your List Box -->
<xp:listBox id="listBox1" value="#{viewScope.yourValue}" multiple="true">
    <xp:selectItem itemLabel="Label One" itemValue="1"></xp:selectItem>
    <xp:selectItem itemLabel="Label Two" itemValue="2"></xp:selectItem>
    <xp:selectItem itemLabel="Label Three" itemValue="3"></xp:selectItem>
    <xp:eventHandler event="onchange" submit="true" refreshMode="partial" refreshId="listBox2"></xp:eventHandler>
</xp:listBox>


<!-- This is instead of your 'Computed Field' -->
<xp:listBox id="listBox2" value="#{viewScope.yourValue}" readonly="true">
    <xp:selectItem itemLabel="Label One" itemValue="1"></xp:selectItem>
    <xp:selectItem itemLabel="Label Two" itemValue="2"></xp:selectItem>
    <xp:selectItem itemLabel="Label Three" itemValue="3"></xp:selectItem>
</xp:listBox>

唯一的缺点是readonly值呈现为垂直表,每行上有每个标签。您可能首选逗号分隔。 您可以将另一个只读控件用于相同的概念,该控件可能支持逗号分隔显示或您喜欢的内容。 或者你可以制作一个自定义渲染器,但那是另一个水壶!

相关问题