我正在研究PrimeFaces AutoComplete演示。我从完整的展示演示中缩短了它。 http://www.primefaces.org/showcase/ui/input/autoComplete.xhtml
AutoCompleteBean.java
@ManagedBean
public class AutoCompleteBean {
private Query query;
private List<Query> queries = new ArrayList<Query>();
@PostConstruct
public void init() {
queries.add(new Query(0, "Afterdark", "afterdark"));
queries.add(new Query(1, "Afternoon", "afternoon"));
queries.add(new Query(2, "Afterwork", "afterwork"));
queries.add(new Query(3, "Aristo", "aristo"));
}
public List<Query> completeQuery(String query) {
List<Query> filteredQueries = new ArrayList<Query>();
for (int i = 0; i < queries.size(); i++) {
Query skin = queries.get(i);
if(skin.getName().toLowerCase().contains(query)) {
filteredQueries.add(skin);
}
}
return filteredQueries;
}
public void onItemSelect(SelectEvent event) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Item Selected", event.getObject().toString()));
}
public Query getQuery() {
return query;
}
public void setQuery(Query query) {
this.query = query;
}
}
Query.java
public class Query {
private int id;
private String displayName;
private String name;
public Query() {}
public Query(int id, String displayName, String name) {
this.id = id;
this.displayName = displayName;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}
我省略了一个转换类,我认为它并不相关。
search.xhtml
<h:form>
<p:growl id="msgs" showDetail="true" />
<h:panelGrid columns="2" cellpadding="5">
<p:autoComplete id="queryPojo" value="#{autoCompleteView.query}"
completeMethod="#{autoCompleteView.completeQuery}" var="query"
itemLabel="#{query.displayName}" itemValue="#{query}"
converter="queryConverter" forceSelection="true" />
<p:commandButton value="search" oncomplete="PF('dlg').show()"/>
</h:panelGrid>
</h:form>
我有三个问题:
1)completeMethod =&#34;#{autoCompleteView.completeQuery}&#34 ;:在不传递参数的情况下调用completeQuery方法,但它被定义为completeQuery(字符串查询)。这是如何工作的?
2)value =&#34;#{autoCompleteView.query}&#34;。 Query是AutoCompleteBean中定义的对象。这个Query对象如何将用户输入字符串作为其值?通常,InputText的值适用于获取用户输入,这是一个字符串值。
3)我还可以添加属性&#34; action = ...&#34;到p:autoComplete componentsnet?
答案 0 :(得分:0)
你在这里省略的转换器类玩真正的游戏....让我们看看你的问题
如您所见,转换器类会覆盖2个方法
getAsString
和getAsObject
1)值
completeMethod="#{autoCompleteView.completeQuery}
被重构为
autoCompleteView.completeQuery(autoCompleteView.query);
你可以在Query类中找到string方法。
2).as转换器定义为自动完成,它调用getAsString方法在屏幕上呈现。选中时,调用getAsObject方法将字符串值转换为object(Query)。
3)你可以使用ajax select event
<p:ajax event="select" listener="#{autoCompleteView.someMethod}">
或通过p:autoComplete
中的onSelect属性调用remoteCommand <p:autoComplete id="queryPojo" value="#{autoCompleteView.query}" onSelect="someRemoteCommand();"
completeMethod="#{autoCompleteView.completeQuery}" var="query"
itemLabel="#{query.displayName}" itemValue="#{query}"
converter="queryConverter" forceSelection="true" />
<p:remoteCommand name="someRemoteCommand" update="queryPojo" actionListener="#{autoCompleteView.execute}" />