This forum is closed to new posts and responses. Individual names altered for privacy purposes. The information contained in this website is provided for informational purposes only and should not be construed as a forum for customer support requests. Any customer support requests should be directed to the official HCL customer support channels below:

HCL Software Customer Support Portal for U.S. Federal Government clients
HCL Software Customer Support Portal



Nov 3, 2011, 8:49 AM
11 Posts
topic has been resolvedResolved

Get the values of a SelectItems tag from SSJS

  • Category: Server Side JavaScript
  • Platform: Windows
  • Release: 8.5.2
  • Role: Developer
  • Tags: Select Items
  • Replies: 8
How do you get the values (not the selected values but the possible values) of a selectitems tag from SSJS ?
 
Background
I have a dialog with several comboboxes, the values for which are set in a selectitems tag by javascript functions. The function always returns at least one default value ('Select Target')
e.g.
                    <xp:panel rendered="#{javascript:showTarget()}">
                        <xp:comboBox id="cbTarget">
                            <xp:selectItems value="#{javascript:getTargetOptions()}" id="siTarget" />
                            <xp:this.validators><xp:validateExpression expression="#{javascript:validateTarget()}" message="Please select target"/></xp:this.validators>
                        </xp:comboBox>
                    </xp:panel>                       

I only want to show the combobox if there are values other than the default 'Select Target'. At present the function showTarget() calls the same function that generates the selectItems getTargetOptions(), checks the results to determine whether to show the panel.
 
As it's inefficient to call the getTargetOptions() function twice I would prefer to call the function once, and then have the showTarget() function check the values of the combobox component to determine whether to render the combobox.
 
function showTarget() {
    var ComboBox = getComponent('cbTarget')
    var ChildrenList:java.util.ListIterator = ComboBox.getChildren().listIterator()
    while (ChildrenList.hasNext()) {
        var Child = ChildrenList.next()
        if (typeof(Child) == 'com.ibm.xsp.component.UISelectItemsEx') {
            //Have handle to SelectItems but unable to get any further
            //as unable to identify which function to use to access the individual select items
            //I suspect it's Child.getValue() but it's an object of a type I don't know and 
            //designer autohelp doesn't recognise either
            //typeof(Child.getValue()) = '[Ljavax.faces.model.SelectItem;'
        }
    }
}
 
 
 
 
 
 
 
 
Nov 3, 2011, 9:59 AM
272 Posts
Re: Get the values of a SelectItems tag from SSJS
Hi,
 
what about a wrapper object? Something like this
 
var optionHelper = {

    targetOptions: null,
    showTarget: function(){
        optionHelper.init();
        if( optionHelper.targetOptions.length > 1 )
                return true;
        return false;
    },
    getTargetOptions: function(){
        optionHelper.init();
        return optionHelper.targetOptions;
    },
    init: function(){
        if( optionHelper.targetOptions == null ){
            optionHelper.targetOptions = getTargetOptions();   
        }
    }

}
 
Instead of calling showTarget() and getTargetOptions() directly, you would call  optionHelper.showTarget() or optionHelper.getTargetOptions(), and your item computing would only exectued once.
 
Sven
 
 
Nov 3, 2011, 11:00 AM
261 Posts
Re: Get the values of a SelectItems tag from SSJS

Or... you could rewrite the getTargetOptions() so the results of that function are temporarily stored in (for instance) the viewScope:
 
function getTargetOptions() {
 if ( !viewScope.targetOptions ) {
 
    //your original code from getTargetOptions here
 
    viewScope.targetOptions = yourTargetOptions;
 }
 
 return viewScope.targetOptions;
 
}
 
By doing so, the code to retrieve/ find the options isn't executed multiple times. You could then set the rendered property of the combobox to (getTargetOptions().length > 1).
 
Mark
Nov 3, 2011, 1:31 PM
11 Posts
Re: Get the values of a SelectItems tag from SSJS
Thanks for the replies, they look like feasible workarounds (will probably need to use request scope rather than viewscope) - however the original question is still unanswered  - how do you get the values of the selectItems component from SSJS ?
 
Nov 3, 2011, 2:43 PM
272 Posts
Re: Get the values of a SelectItems tag from SSJS
Child.getItemLabel() returns the label and Child.getItemValue() returns the value.

function showTarget() {
    var ComboBox = getComponent('cbTarget')
    var ChildrenList:java.util.ListIterator = ComboBox.getChildren().listIterator()
    while (ChildrenList.hasNext()) {
        var Child = ChildrenList.next();
        print( Child.getItemLabel() + "|" + Child.getItemValue() );
    }
}
Nov 3, 2011, 5:23 PM
11 Posts
Re: Get the values of a SelectItems tag from SSJS
That works for a selectitem but not selectitems
 
Unknown member 'getItemLabel' in Java class 'com.ibm.xsp.component.UISelectItemsEx'
Nov 3, 2011, 7:49 PM
129 Posts
Re: Get the values of a SelectItems tag from SSJS
Just a guess, but: 
ComboBox.getChildren().get(0).getChildren().listIterator()
 
This should work if xp:selectItems is the first child of the combo box. 
  
Nov 3, 2011, 8:22 PM
272 Posts
Re: Get the values of a SelectItems tag from SSJS
 
    var ChildrenList:java.util.ListIterator = ComboBox.getChildren().listIterator()
    while (ChildrenList.hasNext()) {
        var Child = ChildrenList.next();     
        if( typeof( Child ) == 'com.ibm.xsp.component.UISelectItemsEx' ){
               var hlp = Child.getValue();
              for( var i=0; i< hlp.length; i++ ){
                print( hlp[i].getLabel() + "|" + hlp[i].getValue() );
             }
        }
        if( typeof( Child ) == 'com.ibm.xsp.component.UISelectItemEx' ){
            print( Child.getItemLabel() + "|" + Child.getItemValue() );
        }
        
    }
 
Nov 4, 2011, 6:46 AM
11 Posts
Re: Get the values of a SelectItems tag from SSJS
Thanks
 
That last one does the job, you've been a great help

This forum is closed to new posts and responses. Individual names altered for privacy purposes. The information contained in this website is provided for informational purposes only and should not be construed as a forum for customer support requests. Any customer support requests should be directed to the official HCL customer support channels below:

HCL Software Customer Support Portal for U.S. Federal Government clients
HCL Software Customer Support Portal