Ahmed Nabil
Ahmed Nabil

Reputation: 18986

rich:autocomplete ‘label’ and ‘value’ of suggestion list?

My target is that
View a suggestion list (bind by objects list in my controller)
Using rich:autocomplete component

A suggestion list items (objects) viewed as userFirstName, userSecondName
When select one item from the list
The label of the suggestion box need to be userFirstName
But
The value of this selected item that will pass to controller need to be userId

My issue is that
When I use this snippet

<rich:autocomplete                      
    autocompleteList="#{myController.myList}"
    var="var" 
    fetchValue="#{var.userFirstName}" 
    value="#{myController.id}"
    autofill="true" 
    layout="table" >

    <rich:column>
        #{var.userFirstName} 
    </rich:column>  
    <rich:column>
        #{var.userSecondName} 
    </rich:column>  

</rich:autocomplete>

The passed value to my controller is userFirstName not userId
The problem is that the value of fetchValue property is the both (the label and the value) So how I can defer between the label and the value of the selected item?
Then i will able to view something in suggestion box and pass other to my controller

Upvotes: 3

Views: 2467

Answers (1)

mabi
mabi

Reputation: 5306

That's not actually possible, I'm afraid.

See this post for an example. The gist is that since <rich:autocomplete> is based on HTML's plain <input type=text>, you can only submit to JSF what has been entered in the text box (the value you define with fetchValue). The hidden state present in RF-3.3's autocomplete is gone.

People, myself included, have been cooking up custom solutions involving either hooking into the rich:autocomplete#onselectitem client side event and writing the id to a <h:inputHidden /> field or integrating a client side suggestion alternative like typeahead.

I hear that the RF guys are going to revamp searching in RF-5, which is taking shape now.

Upvotes: 3

Related Questions