繁体   English   中英

JSF Primefaces豆:如何从SelectOneListBox删除选定的值?

[英]JSF Primefaces Bean: How to delete selected value from SelectOneListBox?

我有一些数据的selectOneListBox。 选择一个值并单击“删除”按钮后,必须删除该值。 我需要在Bean中执行此操作。 我认为问题出在if-Statement或codeValue

我的xhtml:

  <p:selectOneListbox id="list" value="#{codelistBean.codeValue2}"                  style="height:300px;overflow:scroll;margin:1px;width:250px"
    autoUpdate="true">
    <f:selectItems value="#{codelistBean.code2Value}" />
    </p:selectOneListbox>`

我的豆子:

变数

String codeValue;

 private static Map<String, Object> codeValue = new LinkedHashMap<String, Object>();

在这里,我为地图添加了一些值:

codeValue.put(getLabel(), getValue()); 

移除方法

public void removeCode(ActionEvent e) {

        for (Iterator<Map.Entry<String, Object>> it = codeValue.entrySet()
                .iterator(); it.hasNext();) {

            Entry<String, Object> entry2 = it.next();

            if (entry2.getKey().equals(codeValue.get(codeValue2))) {
                it.remove();

            }
        }

    }

最后,我将地图返回给JSF以显示它

public Map<String, Object> getCode2Value() {
        return codeValue;
    }

感谢帮助!

您可以定义一个SelectItem列表,而不是使用静态HashMap:

String codeValue2;
List<SelectItem> codeValue = new ArrayList<SelectItem>();

//getters & setters

它像您的HashMap一样保存键/值对,唯一的区别是值/标签顺序:

codeValue.add(new SelectItem(value,label))

删除功能可以简化:

   public void removeCode(ActionEvent e) {

       SelectItem remove = null;
       for (SelectItem item : codeValue) {
           if (item.getValue().equals(codeValue2)) {
               remove = item;
           }
       }
       codeValue.remove(remove);
    }

请注意,您也可以从方法标头中删除ActionEvent e参数,这不是必需的。

<p:selectOneListbox id="list" value="#{codelistBean.codeValue2}"
                    style="height:300px;overflow:scroll;margin:1px;width:250px"
                    autoUpdate="true">
    <f:selectItems value="#{codelistBean.codeValue}" />
</p:selectOneListbox>

在您的jsf页面上, f:selectItems value="#{codelistBean.codeValue}"指向List<SelectItem>value="#{codelistBean.codeValue2}"代表实际选择。

最后,执行“删除”按钮后,不要忘记更新list

<p:commandButton actionListener="#{codeListBean.removeCode}" 
                 value="REMOVE" update="list"/>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM