删除 NatTable 中的多列

Delete multiple columns in NatTable

我正在使用 NatTable 显示 table 数据,table 可以进行排序和筛选。由于 table 相当大,我还使用了 GlazedList。我需要能够在排序和过滤后删除列。正如我所尝试的,我只能删除 table 的内容,但 header 仍然存在。 header 列嵌套在许多层中,我不知道我是否会影响或触发对其进行刷新。

我的代码主要来自示例并稍作修改:

设置图层:

ModelProvider mp = new ModelProvider();

    // property names of the Person class

    this.propertyNames = new String[this.attributeNames.size() + 1];
    this.propertyNames[0] = "Entry";
    for (int i = 0; i < this.attributeNames.size(); i++) {
        this.propertyNames[i + 1] = this.attributeNames.get(i);
    }

    // mapping from property to label, needed for column header labels
    this.propertyToLabelMap = new HashMap<String, String>();

    for (String str : this.propertyNames) {
        this.propertyToLabelMap.put(str, str);
    }

    IColumnPropertyAccessor<GazEntry> columnPropertyAccessor = new GazColumnPropertyAccessor();

    final BodyLayerStack<GazEntry> bodyLayerStack = new BodyLayerStack<GazEntry>(
            mp.entrylines, columnPropertyAccessor);

    IDataProvider columnHeaderDataProvider =
            new DefaultColumnHeaderDataProvider(this.propertyNames, this.propertyToLabelMap);
    final DataLayer columnHeaderDataLayer =
            new DataLayer(columnHeaderDataProvider);
    final ColumnHeaderLayer columnHeaderLayer =
            new ColumnHeaderLayer(columnHeaderDataLayer, bodyLayerStack, bodyLayerStack.getSelectionLayer());

    SortHeaderLayer<GazEntry> sortHeaderLayer =
            new SortHeaderLayer<GazEntry>(
                    columnHeaderLayer,
                    new GlazedListsSortModel<GazEntry>(
                            bodyLayerStack.getSortedList(),
                            columnPropertyAccessor,
                            configRegistry,
                            columnHeaderDataLayer));

    // build the column header layer

    // Note: The column header layer is wrapped in a filter row composite.
    // This plugs in the filter row functionality
    FilterRowHeaderComposite<GazEntry> filterRowHeaderLayer = new FilterRowHeaderComposite<GazEntry>(
            new DefaultGlazedListsFilterStrategy<GazEntry>(
                    bodyLayerStack.getFilterList(), columnPropertyAccessor,
                    configRegistry), sortHeaderLayer,
            columnHeaderDataLayer.getDataProvider(), configRegistry);

    // build the row header layer
    IDataProvider rowHeaderDataProvider = new DefaultRowHeaderDataProvider(
            bodyLayerStack.getBodyDataProvider());
    DataLayer rowHeaderDataLayer = new DefaultRowHeaderDataLayer(
            rowHeaderDataProvider);
    final ILayer rowHeaderLayer = new RowHeaderLayer(rowHeaderDataLayer,
            bodyLayerStack, bodyLayerStack.getSelectionLayer());

    // build the corner layer
    IDataProvider cornerDataProvider = new DefaultCornerDataProvider(
            columnHeaderDataProvider, rowHeaderDataProvider);
    DataLayer cornerDataLayer = new DataLayer(cornerDataProvider);
    ILayer cornerLayer = new CornerLayer(cornerDataLayer, rowHeaderLayer,
            filterRowHeaderLayer);

    IRowDataProvider<GazEntry> bodyDataProvider = (IRowDataProvider<GazEntry>) bodyLayerStack.getBodyDataProvider();
    bodyLayerStack.setConfigLabelAccumulator(new CrossValidationLabelAccumulator(
            bodyDataProvider));

    // DataLayer bodyDataLayer = new DataLayer(bodyDataProvider);
    bodyLayerStack.registerCommandHandler(new
            DeleteRowCommandHandler<GazEntry>(bodyLayerStack.bodyData));

    //TODO: register delete column.
    bodyLayerStack.registerCommandHandler(new
            DeleteColCommandHandler<GazEntry>(bodyLayerStack.bodyData));

和删除列的命令处理程序

class DeleteColCommandHandler<T> implements ILayerCommandHandler<DeleteColCommand> {

    private List<T> bodyData;

    public DeleteColCommandHandler(List<T> bodyData) {
        this.bodyData = bodyData;
    }

    @Override
    public Class<DeleteColCommand> getCommandClass() {
        return DeleteColCommand.class;
    }

    //TODO: delete column
    @Override
    public boolean doCommand(ILayer targetLayer, DeleteColCommand command) {
        // convert the transported position to the target layer
        if (command.convertToTargetLayer(targetLayer)) {
            // remove the element
            // this.bodyData.remove(command.getRowPosition());
            SelectionLayer slayer = ((BodyLayerStack) targetLayer).getSelectionLayer();
            int[] selected = slayer.getSelectedColumnPositions();

            for (int index : selected) {
                String colName = CopyOf_6031_GlazedListsFilterExample.this.propertyNames[index];
                CopyOf_6031_GlazedListsFilterExample.this.attributeNames.remove(colName);
                targetLayer.fireLayerEvent(new
                        ColumnDeleteEvent(targetLayer, index));
            }
            return true;
        }
        return false;
    }
}

如前所述,这将删除列内容但保留 header。谁能告诉我如何删除 header 列?

您是真的要删除列还是只想隐藏列?因为隐藏会容易得多。当然,这取决于您的用例以及您的数据模型是否可以修改为真正删除列。

然而,DefaultColumnHeaderDataProvider 不支持动态添加或删除列,因为它基于数组。对于这样的用例,您需要为列 header 提供自定义 IDataProvider。 NatTable Examples 应用程序在 Tutorial Examples -> Data -> DynamicColumnExample 下包含一个示例。

您只需要实现一个基于列表而不是数组的 IDataProvider,这样就可以删除元素并修改大小。