问题描述
这是我希望得到答案的另一个JavaFX问题。目前,当您在TableView中对列进行排序时,您将从第一次单击开始按升序排序,从第二次单击开始降序,在第三次单击时,它不执行任何操作,排序箭头将消失。据我所知,第三种排序模式应该将列返回到先前的未排序状态。我知道由于受欢迎的请求,最近添加了这种模式,但有没有办法完全绕过它?也就是说,在第一次点击时我们按升序排序,在第二次点击时我们按降序排序,就是这样,第三次点击将再次按升序排序?有没有简单的方法呢?
Here's another JavaFX question I was hoping to get an answer to. Currently, when you sort a column in TableView, you get ascending order from the first click, descending from the second click, and on a third click it does nothing and the sorting arrow disappears. I understand that the third sorting mode should return the column back to its previous, unsorted state. I know that this mode has been added recently due to popular requests, but is there a way to completely bypass it? That is, on the first click we sort in ascending order, on a second click we sort in descending order, and that's it, the third click will be ascending order again? Is there an easy way to do this?
谢谢
推荐答案
TableView上的sortOrder属性负责保存显示为已排序的列。
考虑到这一点,为了确保列始终排序,可以观察到该属性,并且只要发现它不包含列(例如第三次单击),就可以通过监听器将其添加回来:
The sortOrder property on TableView is responsible for holding the columns that are shown as sorted.With this in mind to ensure a column is always sorted this property can be observed and whenever it is found to not contain the column (such as on the 'third click') then just add it back through a listener as so:
TableColumn<?,?> aColumnThatMustAlwaysBeSorted = ...;
tableView.getSortOrder().addListener( (ListChangeListener<? super TableColumn<?,?>>)observable -> {
if( !tableView.getSortOrder().contains(aColumnThatMustAlwaysBeSorted) ){
aColumnThatMustAlwaysBeSorted.setSortType(SortType.ASCENDING);
tableView.getSortOrder().add(aColumnThatMustAlwaysBeSorted);
}
});
getSortOrder().add(aColumnThatMustAlwaysBeSorted);
当单击另一列时,单击多列进行排序时的行为似乎有些不寻常'always-sorted'列将成为第二个排序索引,任何其他列将在其后追加。如果用户想要在索引的下方进一步向下移动,则单击始终排序列将再次强制将其作为最后一个排序索引附加。
The behaviour when shift clicking multiple columns for sorting can seem a bit unusual at first as on clicking another column the 'always-sorted' column will become the second sort index and any further columns will append after it. If the user wants it further down the index then shift clicking the 'always-sorted' column will again force it to be appended as the last sort index.
这篇关于对TableView列进行排序:绕过“未排序”列的方法模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!