Primefaces数据表过滤器

时间:2013-03-22 14:32:06

标签: java jsf primefaces

我尝试过一些东西,但是我无法对我的过滤器dataTable做任何工作。已经按照primefaces展示的例子,没有。

我有以下代码:

XHTML:

<p:dataTable id="dataTable" var="valor" value="#{beanMensagemXContato.listaContatoEmail}" 
                widgetVar="carsTable" emptyMessage="No cars found with given criteria" filteredValue="#{tableBean.filteredCars}">                           

                <f:facet name="header">  

                    </f:facet> 

                    <p:column 
                        style="max-width: 50px; min-width: 50px; overflow: hidden">
                        <f:facet name="header">
                            <h:outputText value="Contato" />
                        </f:facet>
                        <h:outputText value="#{valor.nomGrupoEmail}" />
                    </p:column>

                    <p:column
                        style="max-width: 50px; min-width: 50px; overflow: hidden">
                        <f:facet name="header">
                            <h:outputText value="Email" />
                        </f:facet>
                        <h:outputText value="#{valor.endEmail}" />
                    </p:column>

                    <p:column
                        style="max-width: 50px; min-width: 50px; overflow: hidden">
                        <f:facet name="header">
                            <h:outputText value="Telefone" />
                        </f:facet>
                        <h:outputText value="#{valor.numTelefone}" />
                    </p:column>

                    <p:column
                        style="max-width: 50px; min-width: 50px; overflow: hidden">
                        <f:facet name="header">
                            <h:outputText value="Ações" />
                        </f:facet>

                    </p:column>


                </p:dataTable>

豆:

public List<ContatoEmail> getListaContatoEmail() {

        listaContatoEmail = new ArrayList<ContatoEmail>();
        listaContatoEmail = consultaContatoEmail.listarContatoEmail();

        return listaContatoEmail;
    }

我想要在dataTable中输入单词时选择行。

有人可以给我一个简单的例子。

因为我已经欣赏了。

黛伯拉

2 个答案:

答案 0 :(得分:3)

好的,这是一个例子: 我将采用流行的汽车示例。

使用案例:在自动完成的每次击键时动态更新数据表

My Facelet:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">
<f:view>
    <h:head />
    <h:body>
        <h:form>
            <p:autoComplete var="carac" converter="carconvertor"
                value="#{testBean.selectedCar}" itemLabel="#{carac.carmodel}"
                itemValue="#{carac}"
                completeMethod="#{testBean.complete}" process="@this"
                onSelectUpdate="idGrid">
                <p:ajax event="keyup" listener="#{testBean.onValueChange}"
                    update="idGrid"></p:ajax>
            </p:autoComplete>

            <p:dataTable value="#{testBean.matchingCarModels}" var="carmatch"
                id="idGrid" converter="carconvertor">
                <p:column headerText="Car Model">
                    <h:outputText value="#{carmatch.carmodel}" />
                </p:column>
            </p:dataTable>
        </h:form>
    </h:body>
</f:view>
</html>

汽车POJO

public class Car 
{
    private String carmodel;

    public Car(String carmodel) {
        super();
        this.carmodel = carmodel;
    }
    // Add setters and getters
}

Car Faces Convertor

@FacesConverter(forClass=Car.class, value="carconvertor")
public class CarConverter 
implements Converter {
    @Override
    public Object getAsObject(FacesContext arg0, UIComponent component, String stringvalue) {
        Car car = new Car(stringvalue);
        return car;
    }

    @Override
    public String getAsString(FacesContext arg0, UIComponent component, Object objectvalue) {
        Car car = (Car) objectvalue;

        if(car == null) {
            return StringUtils.EMPTY;
        }

        return car.getCarmodel();
    }
}

最后是支持bean

@ManagedBean(name="testBean")
@SessionScoped
public class TestBackingBean 
{
    /**
     * The input filter
     */
    private String filter = StringUtils.EMPTY;

    /**
     * Some test data
     */
    private final List<Car> carModels = new ArrayList<Car>() {
        {
            add(new Car("toyota"));
            add(new Car("honda"));
            add(new Car("suzuki"));
            add(new Car("ford"));
            add(new Car("renault"));
            add(new Car("subaru"));
        }
    };

    /**
     * For updating the grid.
     */
    public void onValueChange(AjaxBehaviorEvent event)
    {
        AutoComplete ac = (AutoComplete) event.getSource();

        Car input = (Car) ac.getValue();

        filter = (input == null) ? StringUtils.EMPTY : input.getCarmodel();
    }

    /**
     * For the auto complete drop down
     */
    public List<Car> complete(String input)
    {
        filter = input;

        return getMatchingCarModels();
    }

    /**
     * get the match
     */
    public List<Car> getMatchingCarModels()
    {
        if(StringUtils.isEmpty(filter))
            return carModels;

        List<Car> matches = new ArrayList<Car>();

        for(Car car : carModels)
        {
            if(car.getCarmodel().startsWith(filter))
            {
                matches.add(car);
            }
        }

        return matches;
    }

    /**
     * The selected car
     */
    private Car selectedCar;
    //Add setters and getters for above member
}

HTH

答案 1 :(得分:1)

您可以在stackoverflow here

中看到相同问题的解决方案

作为替代方法(使用自动完成)进行搜索并捕获keyup事件以更新数据表。一个与您的上下文相关的示例:

<p:autoComplete var="address"
    value="#{addressBean.address}" itemLabel="#{address.personName}"
    itemValue="#{address}" completeMethod="#{addressBean.complete}"
    process="@this" converter="personconvertor"
    onSelectUpdate="dataTable">
    <p:ajax event="keyup" listener="#{addressBean.onValueChange}"
        update="dataTable"></p:ajax>
</p:autoComplete>