PrimeFaces: get current sort order of columns in Javascript
I am currently trying to get the current order of the columns in the PrimeFaces object when the user is reordering them. Since the server event doesn't provide any parameters, I decided to use a JavaScript handler for this.
function onColumnReorder(glossary) {
var sortedColumnNames = ...
}
"Glossary" is my widget variable. The question is, how do I now get the column names in their current order? Note that I am not talking about sorting the table data, but simply about the current order in which the columns are displayed in the table (from left to right), which may change since "draggableColumns" is set to true.
Thanks for any advice and best wishes from Pascal
source to share
First, define your colReorder
AJAX event to datatable
:
<p:ajax event="colReorder" listener="#{myBean.onColReorder}"/>
In the method, onColReorder
find your datatable by id using the method UIViewRoot#findComponent()
. Once you've found yours datatable
, it's easy. Use the method getColumns()
to get the list of columns in onColReorder()
:
FacesContext fc = FacesContext.getCurrentInstance();
DataTable myDatatable = (DataTable) fc.getViewRoot().findComponent("your_datatable");
List<UIColumn> columns = myDatatable.getColumns();
source to share
var th =$(myTableWidgetVar.jqId).find('.ui-state-active');
returns you with id myTableId:myColumnId
, so you know which column has sorting enabled.
You can now read the triangle component:
th.find ('u-sortable-column icon');
And check which class it defined:
-
'ui-icon-triangle-1-n'
to sort in ascending order -
'ui-icon-triangle-1-s'
to sort in descending order
You will have a server callback when the sort order changes if you are using LazyDataModel
.
source to share