简体   繁体   English

在Primefaces选取列表中,如何使用Omnifaces Converter查找将项目从TARGET移至SOURCE

[英]In Primefaces picklist, how to find that an item is moved from TARGET to SOURCE, Using Omnifaces Converter

I have looked around and could not find a solution. 我环顾四周,找不到解决方案。

I am using Omnifaces listConverter in the PickList component of Primefaces . 我使用Omnifaces listConverterPickList的组件Primefaces When I move an item from SOURCE to TARGET . 当我将一个项目从SOURCE移到TARGET时 In the backing bean i get the new item ONLY on dualList.getTarget() . 在支持bean中,我仅在dualList.getTarget()上获得新项。

However when I move an item from TARGET to SOURCE . 但是,当我将项目从TARGET移到SOURCE时 In the backing bean I am not able to check whether an item has been removed from the TARGET . 在支持bean中,我无法检查是否已从TARGET中删除了一个项目。

I tried dualList.getSource() - SOURCE does not contains the item/s dropped from TARGET And off course dualList.getTarget() will be empty (assuming that no item is moved from SOURCE to TARGET). 我尝试了dualList.getSource() - SOURCE不包含从TARGET放下的项目,当然, dualList.getTarget()将为空(假设没有项目从SOURCE移到TARGET)。

My question is how can i find that something has been moved from TARGET to SOURCE in the backing bean? 我的问题是我怎么能发现支持bean中的某些东西已从TARGET移到SOURCE

Previously I have created a custom converter and in that, my dualList.getTarget() gives me the updated target (ie old values + new values added + old values removed). 以前,我创建了一个自定义转换器,在dualList.getTarget() ,我的dualList.getTarget()给了我更新后的目标(即旧值+添加的新值+删除的旧值)。 Therefore I can figure it out which values are added, removed and are still present. 因此,我可以弄清楚哪些值被添加,删除并且仍然存在。

But while using Omnifaces listConverter i am not understanding how to achieve it. 但是在使用Omnifaces listConverter我不了解如何实现它。

My code looks like this: 我的代码如下所示:

XHTML XHTML

<p:pickList id="pickList" value="#{assignBenchmarkersBean.dualUserList}" 
            var="users" itemLabel="#{users.username}" itemValue="#{users}" >
    <o:converter converterId="omnifaces.ListConverter" 
                 list="#{assignBenchmarkersBean.dualUserList.source}" />
</p:pickList>

<p:commandButton id="update" value="#{bundle.create}" 
                 styleClass="redButton bigFont" 
                 actionListener="#{assignBenchmarkersBean.update()}" />

Bean 豆角,扁豆

public void update(){
        try{
            prepareRemoveOldAndAddNewUsersList();
           /**some other code **/
        }catch(Exception e){
            FacesMsg.error(ErrorMessage.UPDATE.getValue(), e);
        }
    }

    private void prepareRemoveOldAndAddNewUsersList() {
        for(User user : dualUserList.getTarget()){      //add users
            addUsers.add(user);
        }
        for(User user : dualUserList.getSource()){      //remove users
            if(oldUserList.contains(user)){
                removeUsers.add(user);
            }
        }
    }

User entity 用户实体

@Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof User)) {
            return false;
        }
        User other = (User) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.abc.model.main.User[ id=" + id + " ]";
    }

NOTE: 注意:

If in the converter's list attribute, I put bean.dualList.target , then the whole thing works the other way around. 如果在转换器的list属性中,我放置了bean.dualList.target ,那么整个事情bean.dualList.target的方式工作。

It is better to use ajax transfer event for this goal, as described in PrimeFaces User Guide . 如《 PrimeFaces用户指南》中所述,最好将ajax transfer事件用于此目标。

XHTML: XHTML:

<p:pickList id="pickList" value="#{assignBenchmarkersBean.dualUserList}" 
        var="users" itemLabel="#{users.username}" itemValue="#{users}" >
    <o:converter converterId="omnifaces.ListConverter" 
             list="#{assignBenchmarkersBean.dualUserList.source}" />
    <p:ajax event="transfer" listener="#{pickListBean.handleTransfer}" />
</p:pickList>

Bean: 豆角,扁豆:

public class PickListBean {
    //DualListModel code
    public void handleTransfer(TransferEvent event) {
        //event.getItems() : List of items transferred
        //event.isAdd() : Is transfer from source to target
        //event.isRemove() : Is transfer from target to source
    }
}

This is how I got it worked for me. 这就是我为我工作的方式。

In the implementation of the Omnifaces ListConverter , the getAsObject method is using the list to find the value and update the target and source of dualList . 在Omnifaces ListConverter的实现中, getAsObject方法使用list查找值并更新dualListtargetsource

NOTE : you will get either a new value added in target (List=".getSource") or a new value added in source (List=".getTarget") , as per the List value you have entered. 注意 :您将获得无论是在增加了新的价值target (List=".getSource")或增加了新的价值source (List=".getTarget")按照该List值已输入。

Hence by changing the List from myBean.dualList.getSource to myBean.completeList , I get the updated target and source (ie old values + new values added + old values removed ) . 因此,通过将ListmyBean.dualList.getSourcemyBean.completeList ,可以获取更新的targetsource (即旧值+添加的新值+删除的旧值)

If a user is only concerned with the new value added in target or source and not on if any value is removed, then he should use myBean.dualList.getSource and myBean.dualList.getTarget respectively. 如果用户只关心添加到targetsource的新值,而不关心是否删除了任何值, myBean.dualList.getTarget分别使用myBean.dualList.getSourcemyBean.dualList.getTarget

However if user wants to know the final content as it is in my case, use the whole collection ie myBean.completeList . 但是,如果用户想了解最终的内容,请使用整个集合,即myBean.completeList

In short it is the List which you are passing in the <o:conveter> tag is important. 简而言之,在<o:conveter>标记中传递的List很重要。

Hope it will help!! 希望对您有所帮助!!

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

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