PrimeFaces 8.0 & DataTable's sortBy not working anymore

UI Components for JSF
Post Reply
User avatar
asesovici
Posts: 25
Joined: 13 Mar 2018, 16:35

06 May 2020, 10:54

I am having an issue after trying to update our app to PrimeFaces 8.0. All our dataTables have the ability to save the table's columns state (columns sorting as well) in the DB and load it when rendered. After updating to Primefaces 8.0, the sorting is not restored anymore. I tried replicating it with a basic dataTable example and I'm getting the same problem.

I have a simple dataTable with the sortBy attribute set to a predefined list of SortMeta objects:

Code: Select all

<h:form id="carsForm">
    <f:event type="preRenderComponent" listener="#{dataTableBean.setColumnsSorting}" />
    
    <p:dataTable id="carsTable" var="car" value="#{dataTableBean.cars}" rowKey="#{car.ID}" sortMode="multiple" sortBy="#{dataTableBean.preSortOrder}">
    
      <p:column id="idColumnID" headerText="Id" sortBy="#{car.ID}">
        <h:outputText id="idOutputTextID" value="#{car.ID}" />
      </p:column>
     
      <p:column id="idColumnName" headerText="Name" sortBy="#{car.name}">
        <h:outputText id="idOutputTextName" value="#{car.name}" />
      </p:column>
      
    </p:dataTable>
    
  </h:form>

Code: Select all

@javax.inject.Named
@javax.faces.view.ViewScoped
public class DataTableBean implements Serializable {

	private List<SortMeta> preSortOrder = new ArrayList<>();
	private ArrayList<Car> cars = new ArrayList<>();
	
	@PostConstruct
	public void init() {
		cars.add(new Car(1, "tesla"));
  		cars.add(new Car(2, "togg"));
  }
  
	public void setColumnsSorting() {
		UIViewRoot viewRoot = FacesContext.getCurrentInstance().getViewRoot();
		UIColumn column = (UIColumn) viewRoot.findComponent("carsForm:carsTable:idColumnName");
		SortMeta sm = new SortMeta(column, null, SortOrder.DESCENDING, null);
		preSortOrder.add(sm);
	}
	
	public ArrayList<Car> getCars() {
		return cars;
	}
	
	public List<SortMeta> getPreSortOrder() {
		return preSortOrder;
	}
}
The above code works perfectly with PrimeFaces 7.0. The dataTable is rendered with the Name column sorted descending (togg, tesla).

However, the same code with PrimeFaces 8.0 and a different SortMeta constructor call (due to API change), doesn't work anymore:

Code: Select all

SortMeta sm = new SortMeta(column.getColumnKey(), null, SortOrder.DESCENDING, null);
The column is not sorted anymore when the dataTable is rendered. What am I missing? What changed in the meantime?
The fact that the column attribute has been removed from the SortMeta class doesn't seem to be the problem given that only its columnKey was being used subsequently (as far as I could see).

Thank you.
PrimeFaces 8.0 | JSF 2.2 | Payara 5.194
PrimeNG 9.1.3 | Angular 9.1.12

Melloware
Posts: 3716
Joined: 22 Apr 2013, 15:48

06 May 2020, 13:32

See this ticket: https://github.com/primefaces/primefaces/issues/5614

use sortMeta="" instead of sortBy=""
PrimeFaces Developer | PrimeFaces Extensions Developer
GitHub Profile: https://github.com/melloware
PrimeFaces Elite 13.0.0 / PF Extensions 13.0.0
PrimeReact 9.6.1

User avatar
asesovici
Posts: 25
Joined: 13 Mar 2018, 16:35

06 May 2020, 15:57

Thank you, Melloware! I did not find this reference. It works, you're a lifesaver!
PrimeFaces 8.0 | JSF 2.2 | Payara 5.194
PrimeNG 9.1.3 | Angular 9.1.12

Melloware
Posts: 3716
Joined: 22 Apr 2013, 15:48

06 May 2020, 16:00

no problem!!
PrimeFaces Developer | PrimeFaces Extensions Developer
GitHub Profile: https://github.com/melloware
PrimeFaces Elite 13.0.0 / PF Extensions 13.0.0
PrimeReact 9.6.1

User avatar
asesovici
Posts: 25
Joined: 13 Mar 2018, 16:35

06 May 2020, 17:07

Maybe it would be helpful to add sortMeta in its right (alphabetical) place in the documentation https://primefaces.github.io/primefaces ... /datatable. It would make others try to use it instead.
PrimeFaces 8.0 | JSF 2.2 | Payara 5.194
PrimeNG 9.1.3 | Angular 9.1.12

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 28 guests