How to sort records (with code) in a grouped ListGrid?

后端 未结 2 1017
孤城傲影
孤城傲影 2021-01-07 10:26

This is the scenario: I\'m working with a listgrid that needs to be grouped, and also needs to have its records ordered within each group. I\'ve already used the ListG

2条回答
  •  生来不讨喜
    2021-01-07 10:55

    This is due to sort() being called before rendering the grid, and setRecords() complicates things further.
    Initial rendering of the grid happens along with its parents when rootCanvas.draw() is called (in onModuleLoad or similar).
    As setRecords() can be used to change data set in the grid anytime, it tries to redraw the grid regardless of whether its initial stage or not.

    If in the real scenario, sort is triggered after UI initialization, it should work as given in following code sample.
    Remove the sort() call at the end of the constructor.

    final Access access = new Access();
    
    Button button = new Button("Sort");
    button.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
            // toggle sort direction, using two different ways to do it
    
            SortSpecifier sortSpecifier = access.getSortSpecifier("user");
    
            if (sortSpecifier == null || SortDirection.DESCENDING.equals(sortSpecifier.getSortDirection())) {
                access.sort("user", SortDirection.ASCENDING);
            } else {
                access.setSort(new SortSpecifier[]{
                        new SortSpecifier("user", SortDirection.DESCENDING)
                });
            }
        }
    });
    

    Check http://www.smartclient.com/smartgwt/showcase/#grid_multilevel_sort to see how to use listGrid.setInitialSort().

    Having setRecords() in the constructor could lead to other initialization issues as well.

    Update
    To have the grid grouped by and sorted on load, set an initial sort and a group by field as indicated below.

    // along with other configuration methods, can not use after grid is drawn
    SortSpecifier sortSpecifier = new SortSpecifier("user", SortDirection.ASCENDING);
    setInitialSort(new SortSpecifier[]{sortSpecifier});
    
    // use following instead of groupBy(), which is used to group the grid programmatically
    // groupBy() causes a redraw
    setGroupByField("access");
    

    An overloaded ListGrid.setGroupByField(String... field) method is available to group by multiple fields.

提交回复
热议问题