0

Update Grid Model Before Column Sort occurs

asked 2013-01-25 23:39:38 +0800

charlesfizer gravatar image charlesfizer
21 1

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.

delete flag offensive retag edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2013-01-25 23:48:44 +0800

jj gravatar image jj
638 3

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.

link publish delete flag offensive edit

Comments

I just looked at my old code, ant I extended Column object, and overrided the sort(boolean) method.

jj ( 2013-01-25 23:55:03 +0800 )edit
0

answered 2013-01-28 06:19:02 +0800

samchuang gravatar image samchuang
4084 4

updated 2013-01-28 06:19:59 +0800

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

}

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2013-01-25 23:39:38 +0800

Seen: 30 times

Last updated: Jan 28 '13

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More