[英]Datatable does not sort elements in primefaces
Any idea why the sorting does not work? 知道为什么排序不起作用吗?
<h:form prependId="false">
<p:dataTable value="#{questionBackingBean.questions}"
var="question" id="questionTable" paginator="true" rows="15"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="15,25,50" paginatorPosition="bottom">
<p:column sortBy="#{question.id}">
<f:facet name="header">ID</f:facet>
<h:outputText value="#{question.id}" />
</p:column>
<p:column sortBy="#{question.description}">
<f:facet name="header">Description</f:facet>
<h:outputText value="#{question.description}" />
</p:column>
</h:form>
Question entity 问题实体
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof Question)) {
return false;
}
Question other = (Question) obj;
if (id != other.id) {
return false;
}
return true;
}
Backing bean 支持豆
@ManagedBean
@ViewScoped
public class QuestionBackingBean implements Serializable {
private static final long serialVersionUID = 1L;
@Inject
private QuestionLocal questionBean;
private List<Question> questions;
@Inject
private Question question;
private int questionParamId;
public List<Question> getQuestions() {
questions = questionBean.findAllQuestions();
return questions;
}
public void setQuestions(List<Question> questions) {
this.questions = questions;
}
public Question getQuestion() {
return question;
}
public void setQuestion(Question question) {
this.question = question;
}
public int getQuestionParamId() {
return questionParamId;
}
public void setQuestionParamId(int questionParamId) {
this.questionParamId = questionParamId;
}
public void init() {
question = questionBean.findQuestion(questionParamId);
}
public String addQuestion() {
questionBean.createQuestion(question);
return "../index.xhtml";
}
}
You are returning a new list everytime getQuestions is called, doing this in JSF is a bad practice and slows down your app as getters are called multiple times in request lifecycle especially in data iteration. 每次调用getQuestions时,您都将返回一个新列表,这在JSF中是一种不好的做法,因为在请求生命周期中多次调用getter会减慢您的应用程序的速度,尤其是在数据迭代中。 Cache your questions list in a property, getter should just return the questions not load them. 将问题列表缓存在属性中,getter应该只返回问题而不加载它们。
Your list is sorted actually in apply request values phase but you return a new instance when a getter is called again in latter phases you the sorted list is gone during processing. 您的列表实际上是在“应用请求值”阶段中排序的,但是当您在后面的阶段中再次调用getter时,您将返回一个新实例,您的排序列表在处理过程中消失了。
You should never put business logic in getter 您永远不应该将业务逻辑放在吸气剂中
public List<Question> getQuestions() {
questions = questionBean.findAllQuestions();
return questions;
}
change it to 更改为
public List<Question> getQuestions() {
return questions;
}
and 和
public QuestionBackingBean(){
this.questions = questionBean.findAllQuestions();
};
As you are using @ViewScope the constructor will get called every time you refresh so you have latest data. 当您使用@ViewScope时,每次刷新时都会调用构造函数,以获取最新数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.