Frantisek Kossuth
Frantisek Kossuth

Reputation: 3524

optgroup with Combo Box control

Is it possible to render <optgroup> tags within select by native or ExtLib Combo box control?

I want to use native solution, so getComponent() or validators will work. That disqualifies inline html with jQuery/dojo, i think.

Upvotes: 1

Views: 1010

Answers (2)

Frantisek Kossuth
Frantisek Kossuth

Reputation: 3524

I have found this answer to my question. Computed values for combo box in Domino Designer can return string vectors/arrays, usually as return value of @DbLookup or @DbColumn. While experimenting, I have found you can return native component for select items, including group (which inherits from select item). Following snippet will build what I wanted: grouped hierarchy of items.

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">
    <xp:comboBox id="comboBox1" value="#{viewScope.combo}">
        <xp:selectItems>
            <xp:this.value><![CDATA[${javascript:var itms = new javax.faces.model.SelectItem[2];
var itm = null;
itm = new javax.faces.model.SelectItem();
itm.setLabel("label1");
itms[0] = itm;
itm = new javax.faces.model.SelectItem();
itm.setLabel("label2");
itms[1] = itm;

var g = new javax.faces.model.SelectItemGroup();
g.setLabel("Group");
g.setSelectItems(itms);
g
}]]></xp:this.value>
        </xp:selectItems>
    </xp:comboBox>
</xp:view>

Sample:

Combo sample

Based on this example you can combine many data sources to build computed <optgroup> combos, including backing beans or scoped variables.

Upvotes: 1

Dalie
Dalie

Reputation: 696

There doesn't seem to be native support for the <optGroup> tag jet. However, in contrast to the assumption of disqualifying jQuery/dojo, does this seem to be the solution. Where getComponent(), getValue(), setValue() etc.. still work.

The only thing that needs to be done for every <optGroup> you want, is adding a <xp:selectItem itemLabel="With your optGroup label" itemValue"optGroup1"></xp:selectItem>. When multiple <optGroups>'s are used the itemValue needs to increment by 1. I pasted an example below, hope this helps.

<xp:this.resources>
    <xp:script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" clientSide="true"></xp:script>
</xp:this.resources>
...
<xp:comboBox id="comboBox1" styleClass="optGroup">
    <xp:selectItem itemLabel="Swedish Cars" itemValue="optGroup1"></xp:selectItem>
    <xp:selectItem itemLabel="Volvo" itemValue="volvo"></xp:selectItem>
    <xp:selectItem itemLabel="Saab" itemValue="saab"></xp:selectItem>
    <xp:selectItem itemLabel="German Cars" itemValue="optGroup2"></xp:selectItem>
    <xp:selectItem itemLabel="Mercedes" itemValue="mercedes"></xp:selectItem>
    <xp:selectItem itemLabel="Audi" itemValue="audi"></xp:selectItem>
</xp:comboBox>

<xp:scriptBlock id="scriptBlock1">
    <xp:this.value><![CDATA[
    //Iterate across the <option>'s for which the itemValues begin with "optGroup".
    $('.optGroup option[value^="optGroup"]').each(function(index, node) {
        //Use the actual itemValue ("optGroup1") to get all its siblings until the next
        //<option> is found with (again) an itemValue of "optGroup" (i.e. "optGroup2").
        $('.optGroup option[value="' + node.value + '"]').
            //No harm for last iteration: .nextAll() will be used if the selector is
            //not matched or is not supplied (in this example "optGroup3" won't get a match).
            nextUntil('.optGroup option[value="optGroup' + (index + 2) + '"]').
            //Wrap them in a <optGroup> tag and give it its label (itemLabel).
            wrapAll('<optGroup label="' + node.text + '"></optGroup>');
        //Remove the initial <option> since we no longer need it.
        $(this).remove();   
    });
    ]]></xp:this.value>
</xp:scriptBlock>

<xp:button value="Submit" id="button1">
    <xp:eventHandler event="onclick" submit="true"
        refreshMode="complete">
        <xp:this.action><![CDATA[#{javascript:
        print("Submitting: " + getComponent("comboBox1").getValue() );
        }]]></xp:this.action>
    </xp:eventHandler>
</xp:button>

Upvotes: 1

Related Questions