简体   繁体   English

Vaadin Grid:如何获得排名行

[英]Vaadin Grid: How to have a Ranking row

I'm using a grid to show rankings of players. 我正在使用网格显示球员排名。 The list is sorted by various comparators on the backend but there might be still scenarios where 2 rows have the same ranking. 该列表由后端的各种比较器进行排序,但是可能仍然存在两行具有相同排名的情况。 In the example below the first two rows should have number #1 and the last two should be both #2. 在下面的示例中,前两行应为数字#1,后两行应均为#2。

在此处输入图片说明

I implemented the ranking numbers as a column as follows: 我将排名数字实现为列,如下所示:

rankings.addColumn(this::getRowIndex).setWidth("2em")
            .setResizable(true);

With this method: 使用此方法:

private String getRowIndex(TeamHasFormatRecord thfr)
  {
    index++;
    return String.valueOf(index);
  }

Which it's a basic counter. 这是一个基本的计数器。 The only way I could think of is basically getting each entry and find it's place on the list but I think that might be too heavy and won't escalate well. 我能想到的唯一方法是基本上获取每个条目并将其放在列表中,但是我认为这可能太繁琐且无法很好地升级。

Any suggestions? 有什么建议么?

Update Here's the current comparator: 更新这是当前的比较器:

    Comparator.comparingDouble((TeamHasFormatRecord thfr) -> thfr.getPoints())
              .thenComparingDouble(thfr -> thfr.getMean())
              .thenComparingDouble(thfr -> thfr.getStandardDeviation())
              .reversed()

Create a TreeSet and add the point from each row to the set. 创建一个TreeSet并将每行的点添加到集合中。 Use the length of the set to return the rank. 使用集合的长度返回排名。

(Add 10) {10} -> 1 (加10){10}-> 1

(Add 10) {10} -> 1 (加10){10}-> 1

(Add 0) {10, 0} -> 2 (加0){10,0}-> 2

(Add 0) {10,0} -> 2 (加0){10,0}-> 2

This can be achieved by changing the code a little in your getRowIndex method (I named it getRanking as it is no longer a simple index). 这可以通过在getRowIndex方法中稍微更改代码来实现(我将其命名为getRanking因为它不再是简单的索引)。 The trick is to store the thfr of the current/iterated row for the next row to compare with. 诀窍是存储当前行/迭代行的thfr,以便与下一行进行比较。 If they have the same scoring, don't increment the ranking . 如果他们具有相同的得分,则不要增加排名

// class fields
private TeamHasFormatRecord lastIteratedThfr;
private int ranking = 0;

private String getRanking(TeamHasFormatRecord thfr)
{
    // hasSameScore() will check for equal points, mean, and standardDerivation.
    if(lastIteratedThfr == null || !thfr.hasSameScore(lastIteratedThfr)){
        ranking++;
    }
    lastIteratedThfr = thfr; // store this thfr for the next thfr to compare to
    return String.valueOf(ranking);
}

This solution only works if you sort the grid beforehand and you don't allow resorting from user. 该解决方案仅在您事先对网格排序并且不允许用户使用时才有效。 I think you already did that. 我想你已经做到了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM