richfaces suggestionBox passing additional values to backing bean

末鹿安然 提交于 2019-12-01 05:57:09

(Disclaimer: I'm aware that the question was asked rather long time ago, but maybe this'll help someone with a similar problem...)

Check out this blog post which deals with something similar: RichFaces - SuggestionBox and hidden field

The key is to use <f:setPropertyActionListener value="#{...}" target="#{...}"> wrapped inside <a4j:support event="onselect" ajaxSingle="true">. This can be used to set an additional value for a backing bean when onselect is triggered for the SuggestionBox.

With this approach I managed to create a SuggestionBox that displays (and autocompletes) customers' names but upon selection sets a whole customer object (with several properties; identified by an id) for a bean.

Jim Barrows

Does using <f:parameter tag inside the <rich:suggestionbox work?

Have you looked at this RichFaces suggestionBox demo yet ? There are links under the examples to view the source.

Edit:

Sounds like you need the value of state in your bean before the user types in the suggestionBox. I would use the RichFaces ajax support to pass the value of state to the bean so when the autocomplete method is called is has the state the user selected on the page to populate a list of suburbs.

You can use the <f:parameter tab inside the rich:suggestionbox. My task was filtering a list according to some attribute of the list element, where sometimes that attribute could be ignored. Like, sometimes I want a list of only citrus fruit, and sometimes I want the entire list of available fruit.

In the page:

<rich:suggestionbox usingSuggestObjects="true"
        suggestionAction="#{listBuilder.autocompleteFilterFruit('')}" var="ind"
        for="fruitInput" fetchValue="#{fruit.name}" id="suggestion" >
    <f:param name="constrainInd" value="#{basket.isConstrainedToCitrus}" />

    ...

</rich:suggestionbox>

I had one class (Basket) that knew if the list had to be special-filtered, and another class (ListBuilder) that built the list.

In Basket:

public Boolean getIsConstrainedToCitrus ()
{
    return new Boolean ( logic that answers "is this basket for citrus only" );
}

In ListBuilder:

public List<Fruit> autocompleteFilterFruit (Object arg)
{
    List<Fruit> rtnList = new ArrayList<Fruit> ();

    String suggestion = (String) arg;

    // get the filter control that the page retrieved from the Basket
    //
    Map<String,String> 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;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!