gpt4 book ai didi

java - 在primefaces自动完成组件上制作示例时遇到麻烦

转载 作者:太空宇宙 更新时间:2023-11-04 15:00:12 26 4
gpt4 key购买 nike

我正在学习如何使用primefaces框架。我设置了一个用于制作示例的项目,但在使自动完成组件正常工作时遇到了一些麻烦。我想根据玩家来源显示此自动完成组件上“玩家”的提示,但未显示提示。我没有关于Ajax的经验,也没有关于bean的经验,显然我做错了。感谢您的帮助!

<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui">>
<h:head>
<title>Autocomplete</title>
</h:head>
<h:body>
<h:form>
<p:autoComplete value="#{autoComplBean.selectedPlayer}"
completeMethod="#{autoComplete.complete}" var="p"
itemLabel="#{p.name}" itemValue="#{p}" dropdown="true">
</p:autoComplete>
</h:form>
</h:body>
</html>


import java.util.ArrayList;
import java.util.Iterator;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean
@RequestScoped
public class autoComplBean {

private acPlayer selectedPlayer;
private ArrayList<acPlayer> autoCompleteList;

public autoComplBean() {
selectedPlayer = new acPlayer();
autoCompleteList = new ArrayList();
autoCompleteList.add(new acPlayer("rodri", "martinez", 22));
autoCompleteList.add(new acPlayer("junior", "martinez", 27));
autoCompleteList.add(new acPlayer("rodri", "heredia", 25));
autoCompleteList.add(new acPlayer("juan", "gonzales", 22));
autoCompleteList.add(new acPlayer("prueba", "jugador", 23));
autoCompleteList.add(new acPlayer("otro", "player", 27));
autoCompleteList.add(new acPlayer("auto", "complete", 101));
autoCompleteList.add(new acPlayer("ajax", "primefaces", 29));
}

public ArrayList<acPlayer> complete(String query){
ArrayList<acPlayer> players = readPlayers(query);
return players;
}

public ArrayList<acPlayer> readPlayers(String name){

if(name.equals("")){
return autoCompleteList;
}
else{
ArrayList<acPlayer> result = new ArrayList();
for (acPlayer current : autoCompleteList) {
if(current.getName().startsWith(name)){
result.add(current);
}
}
return result;
}

}

public void setSelectedPlayer(acPlayer selectedPlayer) {
this.selectedPlayer = selectedPlayer;
}

public void setAutoCompleteList(ArrayList<acPlayer> autoCompleteList) {
this.autoCompleteList = autoCompleteList;
}

public acPlayer getSelectedPlayer() {
return selectedPlayer;
}

public ArrayList<acPlayer> getAutoCompleteList() {
return autoCompleteList;
}

}

public class acPlayer {
private String name;
private String surname;
private int age;

public acPlayer() {
name = "";
surname = "";
age = 0;
}

public acPlayer(String name, String surname, int age) {
this.name = name;
this.surname = surname;
this.age = age;
}

// setters getters

最佳答案

您想获得其他信息的工具提示吗?有一个例子:http://www.primefaces.org/showcase/ui/autoCompleteItemtip.jsf

虽然itemValue是在bean中设置的值,而itemLabel是在下拉菜单中显示的文本,但对于工具提示,您需要使用facet name="itemtip"

<p:autoComplete value="#{autoComplBean.selectedPlayer}" 
completeMethod="#{autoComplete.complete}" var="p"
itemLabel="#{p.name}" itemValue="#{p}" dropdown="true">
<f:facet name="itemtip">
#{p.name} #{p.surname} (#{p.age})
</f:facet>
</p:autoComplete>

关于java - 在primefaces自动完成组件上制作示例时遇到麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22661232/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com