简体   繁体   中英

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 . When I move an item from SOURCE to TARGET . In the backing bean i get the new item ONLY on dualList.getTarget() .

However when I move an item from TARGET to SOURCE . In the backing bean I am not able to check whether an item has been removed from the 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).

My question is how can i find that something has been moved from TARGET to SOURCE in the backing bean?

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). 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.

My code looks like this:

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.

It is better to use ajax transfer event for this goal, as described in PrimeFaces User Guide .

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 .

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.

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 ) .

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.

However if user wants to know the final content as it is in my case, use the whole collection ie myBean.completeList .

In short it is the List which you are passing in the <o:conveter> tag is important.

Hope it will help!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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