在JSF下拉列表中禁用一些值

时间:2022-03-04 20:04:37

I have a dropdown list that's generated from a map object, that would be repeated depending of the number of list I get from DB, for example, 5 items from the DB, it would generate 5 dropdown list for each row.

我有一个从map对象生成的下拉列表,它会根据我从DB得到的列表数量重复,例如,从DB中得到5个项目,它会为每一行生成5个下拉列表。

I can get the value of the dropdownlist by assign it to a variable in the list, but what I wanted is, to disable certain selected value from the list.

我可以通过将下拉列表赋给列表中的一个变量来获取下拉列表的值,但是我想要的是,禁用列表中的某些选定值。

For example, if I have in the dropdownlist the following items (1, 2, 3, 4, 5) and if for the first row I select the item 1, then the following dropdownlists would only have the remaining items available/selectable (2,3,4,5).

例如,如果我在下拉列表中有以下项(1、2、3、4、5),如果对于第一行,我选择了项1,那么下面的下拉列表将只有剩下的项可用/可选(2、3、4、5)。

I know I can use <p:ajax>, but is there any idea how to disable the item?

我知道我可以使用 ,但是是否知道如何禁用该项?

<p:dataTable var="tableDefinition" id="tableDefinition" value="#{fileUploadManagedBean.theTableList}" rowIndexVar="tdi">
    <p:column headerText="no" width="2%">
        <h:outputText value="#{tdi + 1}" />
    </p:column>
    <p:column headerText="Header" width="25%">
        <h:outputText value="#{tableDefinition.header}" />
    </p:column>
    <p:column headerText="Sample Value" width="25%">
        <h:outputText value="#{tableDefinition.sampleValue}" />
    </p:column>
    <p:column headerText="Map Field" width="25%">
        <p:selectOneMenu id="fieldList" 
                         value="#{tableDefinition.fieldName}" style="width:150px" >
            <f:selectItem itemLabel="#{msg['form.import.upload_type_select']}" itemValue="" noSelectionOption="true" />
            <f:selectItems value="#{fileUploadManagedBean.fieldTypes}" />
        </p:selectOneMenu>
    </p:column>
</p:dataTable>

Btw just to share with others, here is what I put on my backing bean :

顺便跟大家分享一下,这是我在我的后备豆上写的:

 public void doCheckList(String fieldType){
 Set<String> keySet = selectedFieldTypes.keySet();

    Iterator<String> iterator = keySet.iterator();

    List<String> removeList = new ArrayList();

    while(iterator.hasNext()){
        String selectedField = iterator.next();
        boolean isEqual = false;
        for(int i=0;i<theTableList.size();i++){
            TableDefinition tdef = theTableList.get(i);
            String fieldName = tdef.getFieldName();

            if(selectedField.equals(fieldName)){
                isEqual = true;
            }

            if(i == (selectedFieldTypes.size() - 1) && !isEqual){
                removeList.add(selectedField);
            }

    }
    }

    if(removeList.size()>0){
        for(String fieldToRemove:removeList){
            if(fieldToRemove!=null){
                selectedFieldTypes.remove(fieldToRemove);
            }
        }
    }
    selectedFieldTypes.put(fieldType,fieldType);
 }
public boolean isItemSelected(String fieldType) {       
    return selectedFieldTypes.containsKey(fieldType);
}

And here's what I change for my select one menu :

这是我选择的菜单的改变:

         <p:selectOneMenu  id="fieldList" value="#{tableDefinition.fieldName}" style="width:150px" >
            <f:selectItem itemLabel="#{msg['form.import.upload_type_select']}" itemValue=" " itemDisabled="#{fileUploadManagedBean.isItemSelected(item)}" />

            <f:selectItems value="#{fileUploadManagedBean.fieldTypes}" var="item" itemDisabled="#{fileUploadManagedBean.isItemSelected(item)}" />
            <p:ajax onstart="showAjaxLoading();" oncomplete="hideAjaxLoading();" listener="#{fileUploadManagedBean.doCheckList(tableDefinition.fieldName)}" update=":importData" />
        </p:selectOneMenu>

The problem I'm having now is that the disabled item is detect as null in the backing bean (the value is not passed) even though it's already selected.

我现在遇到的问题是,禁用的项在支持bean中被检测为null(值没有被传递),即使它已经被选中。

1 个解决方案

#1


3  

You can use the itemDisabled property:

您可以使用itemDisabled属性:

<f:selectItem itemLabel="#{msg['form.import.upload_type_select']}" itemValue="" noSelectionOption="true" />
<f:selectItems value="#{fileUploadManagedBean.fieldTypes}" var="item" itemDisabled="#{fileUploadManagedBean.isItemSelected(item)}"/>

And in your backing bean:

在你的备胎里:

public boolean isItemSelected(FieldType fieldType) {
     return selectedFieldTypes.contains(fieldType)
}

#1


3  

You can use the itemDisabled property:

您可以使用itemDisabled属性:

<f:selectItem itemLabel="#{msg['form.import.upload_type_select']}" itemValue="" noSelectionOption="true" />
<f:selectItems value="#{fileUploadManagedBean.fieldTypes}" var="item" itemDisabled="#{fileUploadManagedBean.isItemSelected(item)}"/>

And in your backing bean:

在你的备胎里:

public boolean isItemSelected(FieldType fieldType) {
     return selectedFieldTypes.contains(fieldType)
}