获取列表框的选定值?

时间:2013-01-10 13:38:59

标签: xpages

如何获取所选项目是列表,组合框等?

我找到了这段代码here

/*****
 *** getSelectableValues()
 *** prints all selectable values for a given component, f.e. comboboxes, listboxes etc.
 ***
 *** @params id of component
 *****/

function getSelectableValues( id ) {
   var ComboBox = getComponent( id );
   var ChildrenList:java.util.ListIterator;
   ChildrenList = ComboBox.getChildren().listIterator();
   while (ChildrenList.hasNext()) {
      var Child = ChildrenList.next();

      /*** process computed / multiple values ***/
      if( typeof( Child ) == 'com.ibm.xsp.component.UISelectItemsEx' ){
         var hlp = Child.getValue();
         for( var i=0; i< hlp.length; i++ ){

            /*** print to server console ***/
            print( hlp[i].getLabel() + "|" + hlp[i].getValue() );
         }
      }

      /*** process single values ***/
      if( typeof( Child ) == 'com.ibm.xsp.component.UISelectItemEx' ){

      /*** print to server console ***/
      print( Child.getItemLabel() + "|" + Child.getItemValue() );
      }
   }
}

/*** get all selectable values for element 'comboBox1' ***/
getSelectableValues( 'comboBox1' );

但它似乎获得了列表框中的所有项目,而不仅仅是所选项目。有任何想法如何修改它以获得所选的值?

2 个答案:

答案 0 :(得分:2)

不要询问组件,而是询问数据模型。例如,如果列表框绑定到:

#{someDoc.someItemName}

...然后您可以通过询问数据源来检索所选的值:

var selectedValues = someDoc.getValue("someItemName");

如果组件绑定到范围变量:

#{viewScope.selectedValues}

...然后问这个变量:

var selectedValues = viewScope.get("selectedValues");

答案 1 :(得分:1)

您可以使用SSJS访问所选值:

getComponent('comboBox1').value

如果您正在使用列表框并且启用了多个选择,则可以使用Explode来获取字符串数组:

@Explode(getComponent('listBox1').value)