0
votes

I decided to ask here so I will solve my problem. Before I write to you I can ensure you that I googled a lot, but no answer found.

In my case IN PRIMEFACES, the sortBy, and filterBy in the p:column in p:dataTable, do not work.

Lets start with the sortBy. () In all the versions I tried as you see in the following pom.xml: 6.2, 7.0, 7.0.RC3, 8.0, 8.0.RC3, it has this behavior: It displays the column headerText, with the 2 up/down arrows. When I click to the arrows they DO NOT change "up", after "down". And obviously NO SORTING is taking place. () Only in the versions as you see in the following pom.xml: 6.0, 6.1 it has this behavior: It displays the column headerText, with the 2 up/down arrows. When I click to the arrows they DO change "up", after "down". But again NO SORTING is taking place.

In some posts they said, that I have to apply filtering first, and on the filtered list to make sorting. I tried with the filterBy... No filtering worked, and no sorting... The magic thing is that the filter did not work in all the previously mentioned versions...

At the following I write all the files I used... I use Spring MVC 5.2.1 (Spring beans), Hibernate 5.4.3/JPA, JSF 2.2.20, Primefaces. I tried to reproduce the example of the "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 --------------

In here I have tried only sorting in the first column, and filtering nad sorting in the second column.

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

-------------- Car model --------------

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

Here is the heart of the code... As I wrote I use Spring beans (the first 2 annotations in the SortViewBackingBean). But, I overrode the Spring way, and I used CDI the one time / JSF way the other time, but it did not work as well (the commented annotations in the SortViewBackingBean).

I will show you now how I feed the DataTable with the list "cars". Googling I found out that the sorting can work only if we feed the datatable with the same list. If every time tye list is different, the sorting does not work... That is why, as you see in the following code, in 2 versions... No one of these versions work...

-------------- SortViewBackingBean version 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 version 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;
    }
}

So, ok! These features in Primefaces work, or it is just in me only! Can someone help on this please?

Thanks a lot

1
uhhhh... 8.0RC3 is a realease candidate, it is older than 8.0. Oh and JSF and Spring MCV are 'competing' technologies.Kukeltje
Hey Kukeltje. I tried 8.0 also. I tried ManagedBean also. Nothing works, except of 6.0, 6.1. But these worked at the icons. No sorting as wellMylonas Thomas

1 Answers

0
votes

Based on your code: you need to use a scope longer than request like viewscope or sessionscope (not both) to keep the filteredValue so that filtered list is still accessible after filtering. e.g.

@Named("sortViewBackingBean")
@SessionScoped

if you want to sort more than one column, you need to enable Multiple sorting by setting sortMode to multiple. In this mode, clicking a sort column while metakey is on adds sort column to the order group. e.g.

<p:dataTable var="car" value="#{carBean.cars}" sortMode="multiple">
    //columns
</p:dataTable>