Xpages 复选框以选择列表框的所有值

Xpages 复选框以选择列表框的所有值,xpages,xpages-ssjs,Xpages,Xpages Ssjs,我想要一个复选框,当选中时,选择列表框中的所有值。 要查看是否选中该复选框,我使用: if(getComponent('checkBox1').getValue()=“true”){ //如何选择的所有项目,例如listbox1 }您可以使用listbox组件的setSelectedValues()方法,并传入一个包含listbox所有值的数组,例如[value1”、“value2”、“value3]。将其放在复选框的onChange事件中,部分刷新列表框,这应该可以满足您的需要 如果您不想在S

我想要一个复选框,当选中时,选择列表框中的所有值。 要查看是否选中该复选框,我使用:

if(getComponent('checkBox1').getValue()=“true”){
//如何选择的所有项目,例如listbox1

}

您可以使用listbox组件的
setSelectedValues()
方法,并传入一个包含listbox所有值的数组,例如[value1”、“value2”、“value3]。将其放在复选框的onChange事件中,部分刷新列表框,这应该可以满足您的需要

如果您不想在SSJ中硬编码listbox值,而是从listbox中提取可用值,请查看此项。使用该片段,我制作了以下示例:

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">
<xp:checkBox text="Select All" id="checkBox1">
    <xp:eventHandler event="onchange" submit="true" refreshMode="partial" refreshId="listBox1">
        <xp:this.action>
            <![CDATA[#{javascript:
                //Get the listbox component
                var listbox = getComponent('listBox1');
                if (getComponent('checkBox1').getValue() == "true") {
                    //Checkbox is checked
                    //Get an iterator for the items in the listbox              
                    var childrenList:java.util.ListIterator;
                    childrenList = listbox.getChildren().listIterator();

                    //Generate the array of items
                    var itemList = [];
                    while (childrenList.hasNext()) {
                        var child = childrenList.next();
                        itemList.push(child.getItemValue());
                    }

                    //Set the selectedValues of the listbox
                    listbox.setSelectedValues(itemList);
                }else if(getComponent('checkBox1').getValue() == "false") {
                    //Checkbox is unchecked
                    //Set listbox to have no selections, empty array
                    listbox.setSelectedValues([]);
                }
            }]]>
        </xp:this.action>
    </xp:eventHandler>
</xp:checkBox>
<xp:br />
<xp:listBox id="listBox1" multiple="true" style="height:200px;width:150px">
    <xp:selectItem itemLabel="value1" itemValue="value1"></xp:selectItem>
    <xp:selectItem itemLabel="value2" itemValue="value2"></xp:selectItem>
    <xp:selectItem itemLabel="value3" itemValue="value3"></xp:selectItem>
    <xp:selectItem itemLabel="value4" itemValue="value4"></xp:selectItem>
    <xp:selectItem itemLabel="value5" itemValue="value5"></xp:selectItem>
</xp:listBox>
</xp:view>

您可以使用listbox组件的
setSelectedValues()
方法,并传入一个包含listbox所有值的数组,例如[value1]、[value2]、[value3]。将其放在复选框的onChange事件中,部分刷新列表框,这应该可以满足您的需要

如果您不想在SSJ中硬编码listbox值,而是从listbox中提取可用值,请查看此项。使用该片段,我制作了以下示例:

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">
<xp:checkBox text="Select All" id="checkBox1">
    <xp:eventHandler event="onchange" submit="true" refreshMode="partial" refreshId="listBox1">
        <xp:this.action>
            <![CDATA[#{javascript:
                //Get the listbox component
                var listbox = getComponent('listBox1');
                if (getComponent('checkBox1').getValue() == "true") {
                    //Checkbox is checked
                    //Get an iterator for the items in the listbox              
                    var childrenList:java.util.ListIterator;
                    childrenList = listbox.getChildren().listIterator();

                    //Generate the array of items
                    var itemList = [];
                    while (childrenList.hasNext()) {
                        var child = childrenList.next();
                        itemList.push(child.getItemValue());
                    }

                    //Set the selectedValues of the listbox
                    listbox.setSelectedValues(itemList);
                }else if(getComponent('checkBox1').getValue() == "false") {
                    //Checkbox is unchecked
                    //Set listbox to have no selections, empty array
                    listbox.setSelectedValues([]);
                }
            }]]>
        </xp:this.action>
    </xp:eventHandler>
</xp:checkBox>
<xp:br />
<xp:listBox id="listBox1" multiple="true" style="height:200px;width:150px">
    <xp:selectItem itemLabel="value1" itemValue="value1"></xp:selectItem>
    <xp:selectItem itemLabel="value2" itemValue="value2"></xp:selectItem>
    <xp:selectItem itemLabel="value3" itemValue="value3"></xp:selectItem>
    <xp:selectItem itemLabel="value4" itemValue="value4"></xp:selectItem>
    <xp:selectItem itemLabel="value5" itemValue="value5"></xp:selectItem>
</xp:listBox>
</xp:view>


这正是我想要的。我为最终用户做这件事。选中复选框来选择列表框中的所有项目要比手动选择它们(如果你有很多)快。这正是我想要的。我为最终用户做这件事。选中复选框以选择列表框中的所有项目比手动选择它们(如果有很多)要快