【问题标题】:Primefaces picklist source and target not updatedPrimefaces 选项列表源和目标未更新
【发布时间】:2018-05-08 08:37:35
【问题描述】:

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

当我将用户从源 pickList 移动到目标 pickList(反之亦然),然后更改 dataTable 中的选择时,pickList 源值和目标值与填充时的值保持一致。

用户.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>

【问题讨论】:

  • 请在开发模式下运行您的应用程序,看看是否有任何错误。并且总是创建一个minimal reproducible example 来帮助我们确定问题可能在哪里。现在有很多可能的原因是看不见的。在多个问题中“猜测”或询问更多细节太费时间
  • 我不明白这个问题。 “我希望在提交值之前将这些新值保存在 bean 的对象中” - 什么?您不想将值发送到服务器,但希望它们只出现在服务器上?
  • 我希望能够修改来自多个组的用户,然后一起保存这些更改

标签: primefaces jsf-2


【解决方案1】:

我只是用&lt;form&gt; 包围了我的&lt;pickList&gt; 元素,它就起作用了。如果对您不起作用,请尝试观看此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>

【讨论】:

    【解决方案2】:

    我是这样解决的。

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

    【讨论】:

      【解决方案3】:

      我通过创建Map&lt;Group, Set&lt;User&gt;&gt; 解决了这个问题,然后使用该对象并通过public void onTransfer(TransferEvent event) 方法在每次传输项目时更新它。

      但我仍然不明白getSource()getTarget() 方法在不动态更新时是干什么用的。

      【讨论】:

      • 更改内容时不会将选项列表数据发送到服务器。为此也使用ajax。或者处理 thhttp://stackoverflow.com/questions/25339056/understanding-primefaces-process-update-and-jsf-fajax-execute-render-attributes 中的选项列表。可能会有所作为
      猜你喜欢
      • 1970-01-01
      • 2013-01-10
      • 1970-01-01
      • 1970-01-01
      • 2021-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多