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;
}
}
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 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.