java 如何填充richfaces选项列表的右侧?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/839893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 13:59:59  来源:igfitidea点击:

How to populate the right side of a richfaces picklist?

javajsfrichfaces

提问by Berek Bryan

I am using a Richfaces' picklistand I want to populate the right-side panel with a list of SelectItems from my backing bean.

我正在使用 Richfaces 的选择列表,并且我想用来自我的支持 bean 的 SelectItem 列表填充右侧面板。

Populating the left-side is not a problem from the backing bean, however, the right hand side is problematic.

填充左侧不是来自支持 bean 的问题,但是,右侧是有问题的。

This is what I currently have

这是我目前拥有的

<h:outputText value="Roles" />
<rich:pickList showButtonsLabel="false">
    <f:selectItems value="#{Bean.allRoles}" />
</rich:pickList>

EDIT:

编辑:

So I have roles 'a', 'b', 'c', and 'd'.

所以我有角色“a”、“b”、“c”和“d”。

The user has roles 'a' and 'd', so 'a' and 'd' should be on the right-side panel and 'b' and 'c' should be on the left-side panel.

用户具有角色“a”和“d”,因此“a”和“d”应该在右侧面板上,而“b”和“c”应该在左侧面板上。

EDIT:

编辑:

Further explanation.

进一步说明。

I have three lists for the user.

我有用户的三个列表。

  1. All posible roles (a thru d)
  2. All roles the user is part of (a and d)
  3. All roles the user is NOT part of (b and c)
  1. 所有可能的角色(a 到 d)
  2. 用户属于(a 和 d)的所有角色
  3. 用户不属于(b 和 c)的所有角色

All lists have the data type ArrayList<SelectItem>.

所有列表都具有数据类型ArrayList<SelectItem>

I need the capability to move individual roles between list number 1 and list number 2 and then save the new set of roles. I thought the picklist would be the best richfaces object for the job.

我需要能够在列表编号 1 和列表编号 2 之间移动单个角色,然后保存新的角色集。我认为选择列表将是这项工作的最佳 Richfaces 对象。

采纳答案by Chris Dale

You want this code:

你想要这个代码:

<h:outputText value="Roles" />
<rich:pickList showButtonsLabel="false" value="#{bean.chosenRoles}">
    <f:selectItems value="#{Bean.allRoles}" />
</rich:pickList>

and in your bean you want:

在你的 bean 中,你想要:

private String[] chosenRoles;

+ getter/setter 

Whenver you want to make default roles you just add the roles into the chosenRoles array (for example in the bean constructor). That way chosenRoles will always contain the elements on the right side of the picklist, while elements on the left side are not in the array.

无论何时您想要创建默认角色,您只需将角色添加到 selectedRoles 数组中(例如在 bean 构造函数中)。这样 selectedRoles 将始终包含选项列表右侧的元素,而左侧的元素不在数组中。

Hope this helps!

希望这可以帮助!

回答by indegro

It seems that I found the solution.

看来我找到了解决办法。

<rich:pickList value="#{aBean.rightSideValues}"> 
        <f:selectItems value="#{aBean.leftSideValues}"/>
</rich:pickList>
  • "#{aBean.rightSideValues}"should point to the list or array of objects. With these values right side of the pick list will be populated.

  • #{aBean.leftSideValues}should point to the list of the SelectItem object.

  • "#{aBean.rightSideValues}"应该指向对象列表或数组。将使用这些值填充选择列表的右侧。

  • #{aBean.leftSideValues}应该指向 SelectItem 对象的列表。

ONE NOTICE -- SelectItem object MUST BE constructed with objects from the "#{aBean.rightSideValues}".

一个注意事项 - SelectItem 对象必须由"#{aBean.rightSideValues}".

Example.

例子

class ABean{
   ...
   List<SomeObject> getRightSideValues(){
    ...
  }

   List<SelectItem> getLeftSideValues(){
      List<SomeObjects> someObjects = getAllAvailableObjects();
      List<SelectItem> sItems = new ArrayList<SelectItem>(); 
      for(SomeObject sObj : someObjects){
          SelectItem sItem = new SelectItem(sObj, sObj.toString());
          sItems.add(sItem);
      }
      return sItems;
}

Notice that SelectItem takes first argument and this argument is the reference to the SomeObject. In the internals rich faces will compare objects from the "#{aBean.rightSideValues}"with objects from the #{aBean.leftSideValues}with help of the method

请注意 SelectItem 接受第一个参数,该参数是对 SomeObject 的引用。在内部,丰富的面孔将在方法的帮助下将"#{aBean.rightSideValues}"来自 的对象与来自 的对象 进行比较#{aBean.leftSideValues}

SomeObject.equals()

SomeObject.equals()

回答by Ivar Wed?e

If you need values other than text, you need to make a converter.

如果您需要文本以外的值,则需要制作转换器。

See http://programmingnutsandbolts.blogspot.com/2009/08/richfaces-picklist-converters.html

http://programmingnutsandbolts.blogspot.com/2009/08/richfaces-picklist-converters.html

回答by feder

As in RF 4.3.3 and probably earlier, too, you also need to override the hash method of your model class (entity bean). So, only overriding the equals method for your converter is not enough. The following worked out for me and my PubThread entity bean.

正如在 RF 4.3.3 以及可能更早的版本中一样,您还需要覆盖模型类(实体 bean)的哈希方法。因此,仅覆盖转换器的 equals 方法是不够的。以下内容适用于我和我的 PubThread 实体 bean。

    @Override
    public boolean equals(Object obj) {
        if (obj == null)
            return false;
        if (!(obj instanceof PubThread))
            return false;
        PubThread objectToCheck = (PubThread) obj;
        boolean isShallowCopy = (this.id.equals(objectToCheck.id));
        return isShallowCopy;
    }

    @Override
    public int hashCode() {
        return (this.id.hashCode());
    }

回答by Zack Marrapese

Set the value of the pickList to an array of the value of the SelectItem.

将pickList 的值设置为SelectItem 值的数组。

Whatever you start out with on the right hand side will then basically just be the default value of the pickList:

无论你从右手边开始,基本上都是pickList的默认值:

    <rich:pickList value="#{aBean.rightSideValues}"> 
        <f:selectItems value="#{aBean.leftSideValues}"/>
    </rich:pickList>