views:

3732

answers:

4

When using the rich faces suggestionBox how can you pass more than one id or value from the page with the text input to the suggestionBox backing bean. ie: to show a list of suggested cities within a selected state? Here is my autoComplete method.

public List< Suburb > autocomplete(Object suggest)
{
    String pref = (String) suggest;
    ArrayList< Suburb > result = new ArrayList< Suburb >();

    Iterator< Suburb > 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;
}

As you can see there is one value passed from the page, Object suggest, which is the text of the h:inputText (in the faceLets m:textFormRow )

<m:textFormRow id="suburb" label="#{msgs.suburbPrompt}" 
    property="#{bean[dto].addressDTO.suburb}"
    required="true" maxlength="100" size="30" />

<rich:suggestionbox height="200" width="200" usingSuggestObjects="true"
    suggestionAction="#{suburbsMBean.autocomplete}" var="suburb" for="suburb"
    fetchValue="#{suburb.name}" id="suggestion">
    <h:column>
        <h:outputText value="#{suburb.name}" />
    </h:column>
</rich:suggestionbox>

Earlier in the page you can select a state which I'd like to use to pare down the list of suburbs that the suggestion box displays.

A: 

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.

Mark Robinson
I've used the code from the richfaces demo.
Martlark
A: 

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

Jim Barrows
A: 

I have achived it using ajax.

javagenious
+2  A: 

(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.

Jonik
Here's another SO answer about basically the same problem: http://stackoverflow.com/questions/1681559/richfaces-suggestionbox/1687031#1687031
Jonik