-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hello ZK,
I am wondering if the following feature is possible. Please let me explain my scenario first.
I am trying to implement a paging solution for a grid. The reason for paging is that the data from the database query can contain many thousand results and the time it takes to retrieve and display them is too long. To this end I would implement paging and only retrieve at the required time more data from the database as necessary. I've seen examples on how to implement this and think I can get it implemented.
The issue I am facing is that the customer also would like to be able to sort each column. In order to sort the column, it would be necessary to re-query the database with an appropriate order by clause to return the data in sorted fashion. I believe this is the necessary approach because the list containing results does'nt have the full amount of data, due to the paging. Therefore I need to re-query and get a new list of the paged items in the opposite sort order, perhaps.
From the sort function in the grid, I have not been able to determine a way to first re-popuplate the grid with new data before proceeding to the sort function. Does ZK grid allow this sort of feature?
Please let me know if i need to further explain the issue.
Thank you for any assistance.
The short answer is that you need to sort yourself, which is what you plan on doing anyway. I think you simply need to override the onSort method and put in your implementation (query, retrieving new data (whichi is already sorted) and populate the grid). I've done this way back with zk 3.6. It may be slightly different now.
You can use onSort to reload data from database
zul
<grid id="grid" apply="test.PagingGridSortCtrl" mold="paging" pageSize="20">
<columns>
<column label="Column" sort="auto"></column>
</columns></grid>
Java
public class PagingGridSortCtrl extends SelectorComposer<Grid> {
@Wire
Grid grid;
private boolean ascend = true;
@Override
public void doAfterCompose(Grid grid) throws Exception {
super.doAfterCompose(grid);
grid.setModel(MyDAO.getDataFromDatabase(ascend));
}
@Listen("onSort=column")
public void doSort(SortEvent sortEvt) {
ascend = !ascend;
grid.setModel(MyDAO.getDataFromDatabase(ascend));
Column column = (Column)sortEvt.getTarget();
column.setSortDirection(ascend ? "ascending" : "descending");
sortEvt.stopPropagation();
}
public static class MyDAO {
public static ListModelList<String> getDataFromDatabase(boolean ascend) {
ArrayList<String> list = new ArrayList<String>();
if (ascend) {
for (int i = 0; i < 100; i++) {
list.add("row " + i);
}
} else {
int i = 100;
while (i-- > 0) {
list.add("row " + i);
}
}
return new ListModelList<String>(list);
}
}
}