在primefaces autocomplete组件上制作示例的麻烦

时间:2014-03-26 12:34:24

标签: java ajax jsf primefaces autocomplete

我正在学习如何使用primefaces框架。我设置了一个用于制作示例的项目,并且我在使自动完成组件工作时遇到了一些麻烦。我想向"玩家"显示提示。在此自动完成组件上,它的名称基于玩家的来源,但没有显示提示。我不会在豆子上有很多关于ajax的经验,显然我做错了。谢谢你的帮助!

<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

1 个答案:

答案 0 :(得分:0)

您想获得附加信息的工具提示吗?有一个例子: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>