0

In my XPage I have xp:comboBox with dojoType set to dojox.form.CheckedMultiSelect. When I try to get its value in SSJS using getComponent("comboBox1").getValue() it returns null. If I remove the dojoType then the code works.

Here's the complete code:

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">
    <xp:this.resources>
        <xp:dojoModule name="dojox.form.CheckedMultiSelect"></xp:dojoModule>
        <xp:styleSheet href="/.ibmxspres/dojoroot/dojox/form/resources/CheckedMultiSelect.css"></xp:styleSheet>
    </xp:this.resources>
    <xp:comboBox id="comboBox1" dojoType="dojox.form.CheckedMultiSelect">
        <xp:selectItem itemLabel="Untitled 1"></xp:selectItem>
        <xp:selectItem itemLabel="Untitled 2"></xp:selectItem>
        <xp:selectItem itemLabel="Untitled 3"></xp:selectItem>
    </xp:comboBox>
    <xp:comboBox id="comboBox2">
        <xp:selectItem itemLabel="Untitled 1"></xp:selectItem>
        <xp:selectItem itemLabel="Untitled 2"></xp:selectItem>
        <xp:selectItem itemLabel="Untitled 3"></xp:selectItem>
    </xp:comboBox>
    <xp:button value="Label" id="button1">
        <xp:eventHandler event="onclick" submit="true" refreshMode="complete">
            <xp:this.action><![CDATA[#{javascript:print("============ " + getComponent("comboBox1").getValue());
print("============ " + getComponent("comboBox2").getValue());}]]></xp:this.action>
        </xp:eventHandler>
    </xp:button>
</xp:view>

I the above code comboBox1 has dojoType set to dojox.form.CheckedMultiSelect while comboBox2 is plain combo box. On click of button value of comboBox2 is printed on console while for comboBox1 it prints null.

Why does this happen? What can I do to get the value from comboBox1?

4

2 回答 2

1

I would assume it has something to do with how dojo rewrites this control to achieve this. If you take a look at the source generated, the combo box is something like:

<select>
    <option></option>
    ...
</select>

This is not how the dojo control is rendered it is broken up into many divs inside each other I'm guessing .getValue() just has no idea how to process that. You will need to do this client side with dojo script. The way it seems to work is that inside a number of divs is a div with

data-dojo-attach-point="wrapperDiv"

and inside that div are a number of divs one for each option, the selcted one's div having an attribute aria-selected="true" , you will have to search for this and get the value from inside.

That is unless you can find something in the dojo documentation that details how to pull out the value (I can't)

Update 29-Jun-2012 (Naveen):

Thanks Simon, you were absolutely right about the Dojo rewriting. I was able to get the selected value using client side javascript using the below code:

dijit.byId("#{id:comboBox1}").get("value")

I am putting the selected value in a field using the onClick event and then getting that field value in server side javascript. I know its lot of work for a simple thing, but it works. If you have a better solution then do share.

于 2012-06-29T08:54:16.913 回答
1

The dojoType you refer to, doesn't have a SS getValue() yet. Getting the value CS is however fairly simple using dijit.byId("id").getValue(). With the help of a hiddenInput you have your workaround. I pasted the example below:

<xp:comboBox id="comboBox1" dojoType="dojox.form.CheckedMultiSelect">
    <xp:selectItem itemLabel="Untitled 1"></xp:selectItem>
    <xp:selectItem itemLabel="Untitled 2"></xp:selectItem>
    <xp:selectItem itemLabel="Untitled 3"></xp:selectItem>
</xp:comboBox>

<xp:inputHidden id="inputHidden1"></xp:inputHidden>

<xp:button value="Label" id="button5">
    <xp:eventHandler event="onclick" submit="true"
        refreshMode="complete">
        <xp:this.action><![CDATA[#{javascript:
            print("Submitting: " + getComponent("inputHidden1").getValue());
        }]]></xp:this.action>
        <xp:this.script><![CDATA[dojo.byId("#{id:inputHidden1}").value = dijit.byId("#{id:comboBox1}").getValue();]]></xp:this.script>
    </xp:eventHandler>
</xp:button>
于 2012-06-29T12:11:13.960 回答