2013-01-10 81 views
1

我如何得到選定的項目是一個列表,組合框等?獲取列表框的選定值?

我發現這段代碼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'); 

但它似乎得到所有在列表框中的項目,而不僅僅是選擇的。任何想法如何修改它只獲取選定的值?

回答

1

您可以SSJS訪問選定值:

getComponent('comboBox1').value 

如果您正在使用一個列表框和多選工作已啓用,您可以用爆炸來獲得一個字符串數組:

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

謝謝Sven。我認爲這是@explode做的伎倆。 –

2

而不是詢問組件,詢問數據模型。舉例來說,如果列表框綁定到:

#{someDoc.someItemName} 

...那麼你可以通過詢問數據源檢索所選值:

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

如果組件,而不是綁定到一個作用域變量:

#{viewScope.selectedValues} 

...然後就問那個變量:

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

如果你的盒子連接到你的數據模型,這是更好的方法 –

相關問題