TableView从排序中排除底行(总计) [英] TableView exclude bottom row (total) from sorting

查看:147
本文介绍了TableView从排序中排除底行(总计)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的TableView(Java FX 2.0,但我认为问题相当通用),它获得了默认的排序功能。然而,该表在最后一行中有一个总数,所以我想从排序算法中排除最后一行。

I have a simple TableView (Java FX 2.0, but I suppose the problem is fairly generic) that gets the default sorting feature. However the table has a total in its last line so I would like to exclude that last line from the sorting algorithm.

我找到了一个包含Swing JTable的解决方案为总行创建一个单独的表 - 可以转换为TableView,但它看起来有点累赘。我已经尝试过实现我自己的比较器,但是我认为不可能创建一个既可以升级也可以升级的比较器。降序。

I found a solution for a Swing JTable that consists in creating a separate table for the total row - that would be transposable to a TableView but it seems a bit cumbersome. I have tried implementing my own Comparator but I don't think it is possible to create one that would work in both ascending & descending orders.

推荐答案

使用JavaFX 8可以定义排序策略,问题更容易解决。假设包含总计的行是 TOTAL

With JavaFX 8 it is possible to define a sorting policy, and the problem is easier to fix. Assuming the row containing the totals is TOTAL:

table.sortPolicyProperty().set(t -> {
    Comparator<Row> comparator = (r1, r2) -> 
         r1 == TOTAL ? 1 //TOTAL at the bottom
       : r2 == TOTAL ? -1 //TOTAL at the bottom
       : t.getComparator() == null ? 0 //no column sorted: don't change order
       : t.getComparator().compare(r1, r2); //columns are sorted: sort accordingly
    FXCollections.sort(t.getItems(), comparator);
    return true;
});

这篇关于TableView从排序中排除底行(总计)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆