vaadin网格 - 特定行总是在顶部

发布于 2025-01-21 18:19:14 字数 804 浏览 5 评论 0原文

我有两个类,一个名为“文件夹”,第二个“项目”。我使用一个接口将文件夹和项目一起显示在一个vaadin网格中。我编写了一个自定义比较器以正确排序文件夹和项目(顶部的文件夹)。但是问题在于用户能够更改排序顺序,然后文件夹在底部。 我的问题是如何将文件夹始终放在顶部?是否可以在比较器中检测所选顺序?

grid.addColumn(e -> e.getName())
        .setHeader("Name")
        .setSortable(true)
        .setComparator((item1, item2) -> {
            if (item1 instanceof Folder && item2 instanceof Item) {
                return -1;
            } else if (item1 instanceof Item && item2 instanceof Folder) {
                return 1;
            } else {
                return item1.getName().compareTo(item2.getName());
            }
        })

I have two classes, one named "Folder" and the second one "Item". I'm using an interface to show folders and items together in one Vaadin Grid. I wrote a custom comparator to have folders and items sorted correctly (folders on top). But the problem is that users are able to change the sort order and then the folders are on bottom.
My question is how to have folders always on top? Is it possible to detect the selected order in the comparator?

enter image description here

grid.addColumn(e -> e.getName())
        .setHeader("Name")
        .setSortable(true)
        .setComparator((item1, item2) -> {
            if (item1 instanceof Folder && item2 instanceof Item) {
                return -1;
            } else if (item1 instanceof Item && item2 instanceof Folder) {
                return 1;
            } else {
                return item1.getName().compareTo(item2.getName());
            }
        })

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

染墨丶若流云 2025-01-28 18:19:14

您需要一个比较器,该比较器始终基于类别(您的情况下的文件夹与项目)首次订购,然后按实际的列值订购以解决领域。为了使事情变得更加复杂,无论排序顺序是上升还是下降,都可以使用相同的比较器。您需要通过使类别比较顺序补偿列的排序顺序来弥补这一点,以确保反向订购不会使文件夹持续。

我在下面为此想法实现了一个简单的概念证明,但是如果您可能在那里有零值,或者如果您需要同时支持对多列进行排序,则可能需要对该概念进行一些调整。

@Route
public class View1 extends VerticalLayout {
    record Item(String category, String name) { }

    public View1() {
        Grid<Item> grid = new Grid<Item>();
        grid.addColumn(Item::category).setHeader("Category");
        grid.addColumn(Item::name).setHeader("Name").setSortable(true).setComparator((a, b) -> {
            int categoryOrder = a.category.compareTo(b.category);
            if (categoryOrder == 0) {
                return a.name.compareTo(b.name);
            }
            return grid.getSortOrder().get(0).getDirection() == SortDirection.ASCENDING ? categoryOrder
                    : -categoryOrder;
        });
        grid.setItems(new Item("a", "a"), new Item("a", "c"), new Item("a", "b"), new Item("b", "a"),
                new Item("b", "c"), new Item("b", "b"));

        add(grid);
    }
}

You'd need a comparator that always first orders based on the category (folder vs item in your case) and then order by the actual column value to resolve ties. To make things slightly more complicated, the same comparator is used regardless of whether the sort order is ascending or descending. You would need to compensate for this by making the category comparison order compensate for the sort order of the column to ensure that reverse ordering wouldn't put the folders last.

I implemented a simple proof of concept for this idea below, but you might need to make some tweaks to the concept in case you could have null values there or if you need to support sorting on multiple columns at the same time.

@Route
public class View1 extends VerticalLayout {
    record Item(String category, String name) { }

    public View1() {
        Grid<Item> grid = new Grid<Item>();
        grid.addColumn(Item::category).setHeader("Category");
        grid.addColumn(Item::name).setHeader("Name").setSortable(true).setComparator((a, b) -> {
            int categoryOrder = a.category.compareTo(b.category);
            if (categoryOrder == 0) {
                return a.name.compareTo(b.name);
            }
            return grid.getSortOrder().get(0).getDirection() == SortDirection.ASCENDING ? categoryOrder
                    : -categoryOrder;
        });
        grid.setItems(new Item("a", "a"), new Item("a", "c"), new Item("a", "b"), new Item("b", "a"),
                new Item("b", "c"), new Item("b", "b"));

        add(grid);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文