1

我已经尝试了一些事情,但我无法对我的过滤器数据表做任何工作。已经按照 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 中键入一个单词时选择该行。

谁能给我一个简单的例子。

既然我已经欣赏了。

黛博拉

4

2 回答 2

3

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

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

我的小脸:

<!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
}

A Car Faces 转换器

@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();
    }
}

最后是backing 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
}

高温高压

于 2013-03-24T08:14:48.563 回答
1

您可以在此处查看stackoverflow 中相同问题的解决方案

作为搜索和捕获 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>
于 2013-03-22T14:40:08.273 回答