i want to maintaining sort property on grid.

first i click column on grid. And Sorted as the column.
when i move other page and comeback, it cleared sorted property.
I want to maintaining this sorting.
I used getSortOder(); and setSortOrder(); but parameter was cleared after presenter->service->mapper-> event.
help me please…

when i move other page and comeback - When you leave a view, that view is destroyed and when you then go back to that view, it will be a new instance of that view which means the grid is also a new instance. Therefore, getting the grids sortOrder in that moment will not have the desired result.

Is there still a way to “remember” the sorting? → YES.

You can add a SortListener to the grid, which will write the new SortOrder into the VaadinSession. And whenever your view is attached you check the VaadinSession if there is a saved SortOrder and reapply that to the grid.

grid.addSortListener(sortEvent -> {
    UI.getCurrent().getSession().setAttribute("gridSortOrder", sortEvent.getSortOrder());
});
grid.addAttachListener(attachEvent -> {
    List<GridSortOrder<MyGridItem>> sortOrder = (List<GridSortOrder<MyGridItem>>) UI.getCurrent().getSession().getAttribute("gridSortOrder");
    if (sortOrder != null) {
        grid.sort(sortOrder);
    }
});

Thank you so much Mr.Kaspar.

this solution is working perfectly on my program.

I really appreciate to your help. B)

Kaspar Scherrer:
when i move other page and comeback - When you leave a view, that view is destroyed and when you then go back to that view, it will be a new instance of that view which means the grid is also a new instance. Therefore, getting the grids sortOrder in that moment will not have the desired result.

Is there still a way to “remember” the sorting? → YES.

You can add a SortListener to the grid, which will write the new SortOrder into the VaadinSession. And whenever your view is attached you check the VaadinSession if there is a saved SortOrder and reapply that to the grid.

grid.addSortListener(sortEvent -> {
    UI.getCurrent().getSession().setAttribute("gridSortOrder", sortEvent.getSortOrder());
});
grid.addAttachListener(attachEvent -> {
    List<GridSortOrder<MyGridItem>> sortOrder = (List<GridSortOrder<MyGridItem>>) UI.getCurrent().getSession().getAttribute("gridSortOrder");
    if (sortOrder != null) {
        grid.sort(sortOrder);
    }
});

Version 14
Set the breakpoint in getchildcount. It is found that each time a node is expanded, the getchildcount method will be called multiple times query.getParent () is sometimes null, and sometimes it is the parent node that is actually clicked. This results in that when the parent node is clicked, the returned childcount is 1 (when the parent is null), or the number of real child nodes (such as 12) is retrieved children from BAC The returned value of the kend() method is broken. It is found that the returned data is normal, with 12 items. The final result is that the expansion is the same (same object address) object
It looks like getchildcount is getting the expanded parent node, query.getParent () = = null, what is the problem caused by returning 1 query.getParent () is sometimes null?

HierarchicalDataProvider<FileModel, Void> provider = new AbstractBackEndHierarchicalDataProvider<FileModel, Void>() {

        @Override
        public boolean hasChildren(FileModel item) {
            return true;
        }

        @Override
        public int getChildCount(HierarchicalQuery query) {
            String parentPath = "";
            FileModel file = (FileModel) query.getParent();
            if (file != null) {
                if (file.getParentPath() == null || file.getParentPath().equals("")) {
                    parentPath = file.getName();
                } else {
                    parentPath += file.getParentPath() + "/" + file.getName();
                }
            }
            return fileDao.FolderChild(archiveId, parentPath, null, false).size();
        }

        @Override
        protected Stream<FileModel> fetchChildrenFromBackEnd(HierarchicalQuery query) {
            String parentPath = "";
            FileModel file = (FileModel) query.getParent();
            if (file != null) {
                if (file.getParentPath() == null || file.getParentPath().equals("")) {
                    parentPath = file.getName();
                } else {
                    parentPath += file.getParentPath() + "/" + file.getName();
                }
            }
            List<FileModel> list = fileDao.FolderChild(archiveId, parentPath, null, false);
            String finalParentPath = parentPath;
            list.forEach(i -> {
                i.setArchiveId(archiveId);
                i.setParentPath(finalParentPath);
            });
          return list.stream().limit(query.getLimit()).skip(query.getOffset());
        }
    };