richfaces suggestionBox将附加值传递给辅助bean
使用RichFaces suggestionBox
如何将带有文本输入的页面中的多个id或值传递给suggestionBox
支持bean。 即:显示选定州内的建议城市列表? 这是我的autoComplete
方法。
public List autocomplete(Object suggest) { String pref = (String) suggest; ArrayList result = new ArrayList(); Iterator iterator = getSuburbs().iterator(); while( iterator.hasNext() ) { Suburb elem = ((Suburb) iterator.next()); if( (elem.getName() != null && elem.getName().toLowerCase().indexOf( pref.toLowerCase() ) == 0) || "".equals( pref ) ) { result.add( elem ); } } return result; }
正如您所看到的,页面传递了一个值, Object
建议,这是h:inputText
(在faceLets m:textFormRow
)
在页面的前面,您可以选择一个州,我想用它来削减建议框显示的郊区列表。
(免责声明:我知道很久以前问过这个问题,但也许这会帮助有类似问题的人…)
查看此博客文章,其中涉及类似的内容: RichFaces – SuggestionBox和隐藏字段 。
关键是使用
包含在
。 当为SuggestionBox触发onselect
时,这可用于为辅助bean设置附加值。
通过这种方法,我设法创建一个SuggestionBox,显示(和自动填充)客户的名称,但在选择时为bean设置一个完整的客户对象 (具有几个属性;由id标识)。
在
你看过这个RichFaces的SuggestionBox 演示吗? 示例下有链接来查看源代码。
编辑:
听起来在用户输入suggestionBox之前你需要bean中的state值。 我将使用RichFaces ajax支持将state的值传递给bean,因此当调用autocomplete方法时,用户在页面上选择的状态将填充郊区列表。
您可以使用rich:suggestionbox
的
在页面中:
...
我有一个类( Basket
)知道列表是否必须特殊过滤,另一个类( ListBuilder
)构建列表。
在Basket
:
public Boolean getIsConstrainedToCitrus () { return new Boolean ( logic that answers "is this basket for citrus only" ); }
在ListBuilder中:
public List autocompleteFilterFruit (Object arg) { List rtnList = new ArrayList (); String suggestion = (String) arg; // get the filter control that the page retrieved from the Basket // Map params = FacesContext.getCurrentInstance().getExternalContext ().getRequestParameterMap(); boolean isConstrainedToCitrus = "true".equals (params.get ("constrainInd")); // allFruit is a pre-initialized list of all the available fruit. use it to populate the return list according // to the filter rules and matches to the auto-complete suggestions for (Fruit item : allFruit) { if ((!isConstrainedToCitrus || item.isCitrus()) && item.name.startsWith(suggestion)) { rtnList.add (item); } } return rtnList; }