sorting - Primefaces datatable sort order -
i using primefaces' datatable component display list of classes 'student' :
class student { string firstname string lastname .... }
i populate list form native query. datatable component follows :
<p:datatable id="studentlist" var="student" rowindexvar="rowindex" value="#{studentbean.studentlist}" rowkey="#{student.firstname}" widgetvar="slist" sortby="#{student.firstname}" sortorder="ascending">
and have button refresh data in table. problem is, when refresh data, sorting order lost. how can retain sorting order?
the problem sorting applied when click sorting icon. work around can call methods in datatable component to, first know last "sorted column" , can use primefaces datatable sorting feature. force datatable sorting whenever want:
/** * code first find current order , apply sorting process data. * implemented looking @ primefaces code , doing same steps on sorting (like default database sort) * , decide current order (like in column rendering decide column arrow style). */ protected void refresttablecomponentorder(datatable table) { facescontext facescontext = facescontext.getcurrentinstance(); //when view not created, on first call when preparing model, table not found if(table == null){ return; } valueexpression tablesortbyve = table.getvalueexpression("sortby"); string tablesortbyexpression = tablesortbyve.getexpressionstring(); //loop on children, columns, find 1 order must applied. (uicomponent child : table.getchildren()) { column column = (column)child; valueexpression columnsortbyve = column.getvalueexpression("sortby"); if (columnsortbyve != null) { string columnsortbyexpression = columnsortbyve.getexpressionstring(); if (tablesortbyexpression != null && tablesortbyexpression.equals(columnsortbyexpression)) { //now sort table content sortfeature sortfeature = new sortfeature(); sortfeature.sort(facescontext, table, tablesortbyve, table.getvar(), sortorder.valueof(table.getsortorder().touppercase(locale.english)), table.getsortfunction()); break; } } } }
you can find datatable component anywhere calling:
facescontext facescontext = facescontext.getcurrentinstance(); datatable table = (datatable)facescontext.getviewroot().findcomponent(":your_table_client_id");
Comments
Post a Comment