如何解决在Primefaces中,数据表在列
我决定在这里提问,以便解决我的问题。在我写信给你之前,我可以确保你 我在Google上搜索了很多,但没有找到答案。
在我的情况中,在p:dataTable的p:列中的sortBy和filterBy无法正常工作。
让我们从sortBy开始。 () 在以下pom.xml中,我尝试了所有版本:6.2、7.0、7.0.RC3、8.0、8.0.RC3, 它具有以下行为: 它显示带有两个向上/向下箭头的列headerText。当我单击箭头时,他们 在“向下”之后,请勿更改“向上”。显然没有排序。 () 仅在以下pom.xml:6.0、6.1中看到的版本中,才具有此行为: 它显示带有两个向上/向下箭头的列headerText。当我单击箭头时,他们 不要在“向下”之后更改“向上”。但是再次没有排序。
他们在一些帖子中说,我必须首先应用过滤,然后在过滤列表上 进行排序。我尝试使用filterBy ...没有过滤功能,也没有排序... 不可思议的是,过滤器无法在所有前面提到的版本中使用...
在下面,我写出我使用过的所有文件... 我使用Spring MVC 5.2.1(Spring bean),Hibernate 5.4.3 / JPA,JSF 2.2.20,Primefaces。 我试图重现“ Primefaces Showcase / DataTable / Sorting”的示例。
-------------- pom.xml --------------
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.2.20</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.2.20</version>
</dependency>
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>8.0.RC3</version>
<!-- <version>8.0</version> -->
<!-- <version>7.0</version> -->
<!-- <version>7.0.RC3</version> -->
<!-- <version>6.0</version> -->
<!-- <version>6.1</version> -->
<!-- <version>6.2</version> -->
</dependency>
-------------- page_table.xml --------------
在这里,我仅尝试在第一列中进行排序,并在其中过滤nad进行排序 第二列。
<h:form>
<p:dataTable var="car" value="#{sortViewBackingBean.cars}">
<f:facet name="header">
Single Column Sort
</f:facet>
<p:column headerText="Id" sortBy="#{car.id}">
<h:outputText value="#{car.id}"/>
</p:column>
<p:column headerText="Year" sortBy="#{car.year}" filterBy="#{car.year}">
<h:outputText value="#{car.year}"/>
</p:column>
</p:dataTable>
</h:form>
--------------汽车模型--------------
public class Car {
private String id;
private String brand;
private int year;
private String color;
private int price;
private boolean soldState;
// Constructors,Getters,Setters
}
-------------- CarService --------------
@Service(value = "carService")
public class CarService {
private final static String[] colors;
private final static String[] brands;
// Populate colors,brands
public List<Car> createCars(int size) {
List<Car> list = new ArrayList<>();
for (int i = 0; i < size; i++) {
list.add(new Car(getRandomId(),getRandomBrand(),getRandomYear(),getRandomColor(),getRandomPrice(),getRandomSoldState()));
}
return list;
}
// Other methods
}
-------------- CarService --------------
这是代码的核心... 如我所写,我使用Spring Bean(SortViewBackingBean中的前2个注释)。 但是,我取代了Spring方式,一次使用CDI,另一次使用JSF, 但效果不佳(SortViewBackingBean中带有注释的注释)。
我现在将向您展示如何使用列表“ cars”来提供数据表。 谷歌搜索我发现排序只有在我们以相同的方式喂入数据表时才能起作用 清单。如果每次轮胎列表都不相同,则排序将无法进行... 这就是为什么,如您在以下代码中看到的那样,有2个版本... 这些版本中没有一个可以使用...
-------------- SortViewBackingBean版本1 --------------
@Component(value = "sortViewBackingBean")
@Scope(value = WebApplicationContext.SCOPE_REQUEST,proxyMode = ScopedProxyMode.TARGET_CLASS)
/*
I overrode the Spring way,and I used CDI/JSF way,but it did not work as well
@Named("sortViewBackingBean")
@ManagedBean("sortViewBackingBean")
@ViewScoped
@SessionScoped
*/
public class SortViewBackingBean implements Serializable {
private final List<Car> cars;
private CarService carService;
public SortViewBackingBean(CarService carService) {
this.carService = carService;
// At creation of the bean,the cars is created once only!
cars = this.carService.createCars(10);
}
@PostConstruct
public void init() {
// Or this version,as is in the primefaces showcase
// cars = carService.createCars(10);
}
public List<Car> getCars() {
return cars;
}
public void setService(CarService carService) {
this.carService = carService;
}
}
-------------- SortViewBackingBean版本2 --------------
@Component(value = "sortViewBackingBean")
@Scope(value = WebApplicationContext.SCOPE_REQUEST,proxyMode = ScopedProxyMode.TARGET_CLASS)
public class SortViewBackingBean implements Serializable {
private List<Car> cars;
private CarService carService;
public SortViewBackingBean(CarService carService) {
this.carService = carService;
}
// Or this version I found googling,the cars is created once only!
public List<Car> getCars() {
if (cars == null) {
cars = this.carService.createCars(10);
}
return cars;
}
public void setService(CarService carService) {
this.carService = carService;
}
}
好吧! Primefaces中的这些功能有效,或者仅在我体内! 有人可以帮忙吗?
非常感谢
解决方法
根据您的代码:您需要使用比要求更长的范围,例如viewscope或sessionscope(不能同时使用两者)来保持filteredValue,以便在过滤后仍可访问过滤后的列表。 例如
@Named("sortViewBackingBean")
@SessionScoped
如果要对多个列进行排序,则需要通过将sortMode设置为Multiple来启用Multiple sorting。在此模式下,在metakey处于打开状态时单击排序列会将排序列添加到订单组。 例如
<p:dataTable var="car" value="#{carBean.cars}" sortMode="multiple">
//columns
</p:dataTable>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。