dmitrievanthony
dmitrievanthony

Reputation: 1561

Sorting in primefaces datatable not working

I use sorting in primefaces datatable and it's does not work with paginator. Where I made ​​a mistake?

<p:column headerText="Year" sortBy="#{questionnaireListBean.getQuestionnaireData(questionnaire, 'YEAR')}">
    #{questionnaireListBean.getQuestionnaireData(questionnaire, 'YEAR')}
</p:column>

And view scoped bean with init data in @PostConstruct method:

public String getQuestionnaireData(Questionnaire questionnaire, String column) {
    return questionnairesData.get(questionnaire).get(column);
}

Upvotes: 2

Views: 7675

Answers (1)

Akheloes
Akheloes

Reputation: 1372

So the true title of your question is : "Sorting and pagination not working together in lazy loading datatable, primefaces", that's more of a precise description of your problem.

As for the issue, it apprears that you should expect the problem. In this link, the question was "Is there any datatable JSF component than can perform lazy load pagination, and filtering and sorting on server side. If I need to implement my own solution thanks to the teams that made client side sorting and filtering, they are useless", to which the answer came "No, there isn't. Because the component library cannot know what will be the persistence mechanism.". Of course that dated from 2010...

Taking a look into Primefaces user guide 3.5, it apprears that sorting/paginator/lazy loading can cohabit, but that's more elaborte then just adding sortBy to your columns. In fact, checking page 144 of the guide, you can see that you need to :

  • have a LazyDataModel object in your bean ;
  • Override the load method of this object ;
  • Bind the value of your datatable to this model.

Doing this, you might have sorting along with lazy loading. Haven't tried it, but this seems to adress your issue.

Best of luck.

Upvotes: 3

Related Questions