【问题标题】:Added row moves to last position once filter is removed删除过滤器后,添加的行移动到最后一个位置
【发布时间】:2023-04-06 23:28:01
【问题描述】:

在 NatTable 中,我在过滤表中添加一行。删除过滤器后,新添加的行将移至表格中的最后一个位置。

但我希望它保持在相同的位置,即在过滤表格时添加的行旁边。

我目前正在使用RowInsertCommand。我不想通过用于填充表格的模型或列表添加行。我只想通过 NatTable 命令来实现。有可能吗?

【问题讨论】:

    标签: eclipse eclipse-plugin eclipse-rcp nattable


    【解决方案1】:

    如果没有示例代码,总是很难理解解释和问题。但我假设您只是从 NatTable 示例中复制代码,因此我将根据此解释您的问题。

    首先,RowInsertCommand 有几个构造函数。如果您使用的构造函数没有 rowIndexrowPosition 参数,则新对象将始终添加到列表末尾。

    在将 NatTable 中的过滤器功能与 GlazedLists 一起使用时,包裹在正文 DataLayer 中的列表是 FilterList。如果您在FilterList 上操作以计算应添加新对象的rowIndex,并且将FilterList 作为RowInsertCommandHandler 中的基本列表,则添加新对象的位置在@ 之间转换987654330@ 和基数 EventList,这可能不是我们想要的结果。

    要解决这个问题,您需要使用基础EventList 创建RowInsertCommandHandler

    EventList<T> eventList = GlazedLists.eventList(values);
    TransformedList<T, T> rowObjectsGlazedList = GlazedLists.threadSafeList(eventList);
    SortedList<T> sortedList = new SortedList<>(rowObjectsGlazedList, null);
    this.filterList = new FilterList<>(sortedList);
    
    this.baseList = eventList;
    bodyDataLayer.registerCommandHandler(new RowInsertCommandHandler<>(this.baseList));
    

    那么执行加法操作的动作当然需要根据基数EventList计算索引。以下代码是IMenuItemProviderSelectionAdapter 的一部分:

    int rowPosition = MenuItemProviders.getNatEventData(event).getRowPosition();
    int rowIndex = natTable.getRowIndexByPosition(rowPosition);
    
    Object relative = bodyLayerStack.filterList.get(rowIndex);
    int baseIndex = bodyLayerStack.baseList.indexOf(relative);
    
    Object newObject = new ...;
    natTable.doCommand(new RowInsertCommand<>(baseIndex + 1, newObject));
    

    【讨论】:

    • 感谢您的回复。抱歉,我没有提供我正在使用的构造函数。我正在使用'new RowInsertCommand(ILayer layer, int rowPosition, T... objects)'
    • 我尝试了代码,它按预期工作。谢谢先生。
    猜你喜欢
    • 2015-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-30
    • 1970-01-01
    • 2016-05-28
    • 2022-01-12
    • 1970-01-01
    相关资源
    最近更新 更多