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

时间:2017-11-24 10:37:18

标签: primefaces jsf-2

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

当我将用户从源pickList移动​​到目标pickList(反之亦然),然后更改dataTable中的选择时,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>

2 个答案:

答案 0 :(得分:0)

我通过创建Map<Group, Set<User>>然后使用此对象并通过public void onTransfer(TransferEvent event)方法更新每件商品来解决此问题。

但是,当它们未动态更新时,我仍然不明白getSource()getTarget()方法是什么。

答案 1 :(得分:0)

我是这样解决的。

我的 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());
            }
        }
    }