gpt4 book ai didi

java - Primefaces 数据表过滤器

转载 作者:行者123 更新时间:2023-11-29 03:35:57 24 4
gpt4 key购买 nike

我已经尝试了一些方法,但我无法对我的过滤器数据表做任何工作。已经按照 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>

bean 类:

public List<ContatoEmail> getListaContatoEmail() {

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

return listaContatoEmail;
}

我想要一些东西,当你在 dataTable 中输入一个词时选择该行。

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

因为我已经很感激了。

黛博拉

最佳答案

好的,这是一个例子:我将以汽车为例。

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

我的 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
}

A Car Faces Converter

@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

关于java - Primefaces 数据表过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15572920/

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