Rajat Gupta
Rajat Gupta

Reputation: 26597

Is this a bug in primefaces autocomplete?

I'm trying to put an autocomplete that fetches suggestions as a list of Entry<String, Integer>

<p:autoComplete completeMethod="#{suggester.suggestTopics}"
            var="x1" itemLabel="#{x1.key}" itemValue="#{x1.value.toString()}" 
            value="#{topicController.selected}"   />

Manged bean code is as follows:

private int selected;

public int getSelected() {
    return selected;
}

public void setSelected(int selected) {
    this.selected= selected;
}

But this fails saying the Integer class doesn't have method/property named key. If I remove the value attribute from autocomplete then it starts working properly. But when I put value attribute it starts expecting that the object inside var should be of the same type as that inside value attribute. I believe/expect it should be that the object inside itemValue should be of the same type as that inside value attribute.

I want to use POJOs for suggestions but pass just the entity Id to the value


Using : Primefaces 3.1 JSF 2.1.6

Upvotes: 0

Views: 3725

Answers (4)

iozee
iozee

Reputation: 1349

I know the question is outdated but I've had the same problem. The point is that you have to assign var to p (var="p"). I think it's terribly unobvious (documentation doesnot mention it has to be that way) 'cause I thought I can assign any var name I want.

Upvotes: 0

baba.kabira
baba.kabira

Reputation: 3171

As commented to Matt you dont need to rebuild Player(Pojo) from Db. You can set simply id property of Player(Pojo) and in action method may be utilize this id to fetch it from DB.

In your case in convertor you might do

Entry<String, Integer> e = new Entry<String, Integer>();
e.setId(value)  // where value is passed in to convertor in method getAsObject.....

This value will be set to private Entry<String, Integer> selected

I have used Pojo autocomplete but not tried with generic classes.

Hope this helps.

Upvotes: 0

Matt Handy
Matt Handy

Reputation: 30025

I believe/expect it should be that the object inside itemValue should be of the same type as that inside value attribute.

Yes this makes sense, and it is the same in the primefaces showcase:

<p:autoComplete value="#{autoCompleteBean.selectedPlayer1}" 
                id="basicPojo" 
                completeMethod="#{autoCompleteBean.completePlayer}"  
                var="p" itemLabel="#{p.name}" itemValue="#{p}" 
                converter="player" forceSelection="true"/>  

As you see is var="p" and itemValue="#{p} where p is an instance of Player. And selectedPlayer1 is also an instance of Player.

I don't know if it works with a Map since the Primefaces example is called "Pojo support" and the suggestions should be a List of elements of the same type as in the value attribute.

Upvotes: 1

Daniel
Daniel

Reputation: 37061

I think you want to use the Simple auto complete , but instead you looked at the wrong example on the showcase of the Pojo Support

x1 refers to the int selected - while it expect to be referred to a POJO (with key and value properties.) , that's why you get the message

Integer class doesn't have method/property named key

Or simple use the Simple auto complete

Upvotes: 0

Related Questions