Primefaces 选择列表源和目标未更新

Primefaces picklist source and target not updated

当我从dataTable中select一组时,我想在pickList中显示相应的用户。当我在 pickList 中移动一些用户时,我希望这些新值甚至在提交值之前或在更改 dataTable 中的 selection 之前保存在 bean 的对象中。

当我将用户从源 pickList 移动到目标 pickList(反之亦然),然后更改 dataTable 中的 selection 时,pickList 源和目标值与填充时的值保持相同.

User.java

package com.test.model;

public class User {
    private String userName;

    public User(String userName) {
        this.userName = userName;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
}

Group.java

package com.test.model;

import java.util.List;

public class Group {
    private String groupName;
    private List<User> users;

    public Group(String groupName, List<User> users) {
        this.groupName = groupName;
        this.users = users;
    }

    public String getGroupName() {
        return groupName;
    }

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }

    public List<User> getUsers() {
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }
}

UserBean.java

package com.test.beans;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import com.test.model.Group;
import com.test.model.User;
import org.primefaces.model.DualListModel;

@ManagedBean
@ViewScoped
public class UserBean {
    private List<Group> groups = new ArrayList<>();
    private List<User> allUsers = new ArrayList<>();
    private Group selectedGroup;
    private DualListModel<User> users = new DualListModel<>();

    public List<Group> getGroups() {
        return groups;
    }

    public Group getSelectedGroup() {
        return selectedGroup;
    }

    public void setSelectedGroup(Group selectedGroup) {
        this.selectedGroup = selectedGroup;
    }

    public DualListModel<User> getUsers() {
        return users;
    }

    public void setUsers(DualListModel<User> users) {
        this.users = users;
    }

    @PostConstruct
    private void init()  {
        List<User> usersGroup1 = new ArrayList<>();
        usersGroup1.add(new User("User1"));
        usersGroup1.add(new User("User2"));

        List<User> usersGroup2 = new ArrayList<>();
        usersGroup2.add(new User("User3"));
        usersGroup2.add(new User("User4"));

        allUsers.addAll(usersGroup1);
        allUsers.addAll(usersGroup2);

        groups.add(new Group("Group1", usersGroup1));
        groups.add(new Group("Group2", usersGroup2));
    }

    public void onRowSelect() {
        // here I want to save the picklist source, but it is already reset
        List<User> storeUsers = users.getSource();
        fillPickList();
    }

    private void fillPickList() {
        List<User> assignedUsers = new ArrayList<>();
        List<User> availableUsers = new ArrayList<>();

        assignedUsers.addAll(selectedGroup.getUsers());
        availableUsers.addAll(allUsers);

        for (User user : assignedUsers) {
            if (availableUsers.contains(user))
                availableUsers.remove(user);
        }

        users.setSource(assignedUsers);
        users.setTarget(availableUsers);
    }
}

index.xhtml

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui">

<h:head>
    <link rel="shortcut icon" type="image/x-icon" href="#{resource['images/favicon.ico']}"/>
</h:head>

<h:body>
    <h:form>
        <p:dataTable value="#{userBean.groups}"
                     var="group"
                     selectionMode="single"
                     selection="#{userBean.selectedGroup}"
                     rowKey="#{group.groupName}">
            <p:column headerText="Groups">
                <h:outputText value="#{group.groupName}"/>
            </p:column>
            <p:ajax event="rowSelect"
                    listener="#{userBean.onRowSelect}"
                    update="@form"/>
        </p:dataTable>

        <p:pickList value="#{userBean.users}"
                    var="user"
                    itemLabel="#{user.userName}"
                    itemValue="#{user.userName}"/>
    </h:form>
</h:body>
</html>

我通过创建 Map<Group, Set<User>> 然后使用此对象并通过 public void onTransfer(TransferEvent event) 方法在每次项目传输时更新它来解决它。

但我还是不明白 getSource()getTarget() 方法在没有动态更新的情况下有什么用。

我是这样解决的

我的 xhtml:

<p:pickList id="pickObjList" value="#{myMB.objectList}" var="valor" itemLabel="#{valor}" itemValue="#{valor}" >
                            <p:ajax event="transfer" listener="#{manutParamMB.onAlterarPickList}" update="pickValorList"/>
                            <f:facet name="sourceCaption">Remove</f:facet>
                            <f:facet name="targetCaption">Add</f:facet>
                        </p:pickList>

我的 ManagedBean:

public void onAlterarPickList(TransferEvent event) {
        for(Object item : event.getItems()) {
            
            if(event.isRemove()) {
                objectList.remove(item);
                log.info("Removed:"+item.toString());
            }
            else if(event.isAdd()) {
                objectList.add(item.toString());
                log.info("Added:"+item.toString());
            }
        }
    }

我只是用 <form> 包围了我的 <pickList> 元素并且它起作用了。如果对您不起作用,请尝试观看此 youtube video,因为这个人工作得很好。这是我的 xhtml 代码:

<h:form>
    <p:pickList id="pickSkill"  value="#{curriculumBean.argomenti}" var="argomenti"
        itemLabel=" #{argomenti}" itemValue=" #{argomenti}"
        showCheckbox="true" showSourceControls="false"
        showTargetControls="false">
                <p:ajax event="transfer"
                    listener="#{curriculumBean.onTransfer}" update=":curriculumTab:list:growl" />
    </p:pickList>                                       
</h:form>