简体   繁体   English

删除NatTable中的多列

[英]Delete multiple columns in NatTable

I'm using NatTable to display table data, the table can be sorted and filtered. 我正在使用NatTable来显示表数据,可以对表进行排序和过滤。 Since the table is quite large I also used GlazedList. 由于表很大,因此我也使用了GlazedList。 I need to be able to remove columns after sorting and filtering. 我需要能够在排序和过滤后删除列。 As I tried, I could only remove the content of the table but the header remains there. 正如我尝试的那样,我只能删除表的内容,但标头仍保留在那里。 The column header is nested in many layers and I don't know I could affect or trigger a refresh on it. 列标题嵌套在许多层中,我不知道会影响还是触发它的刷新。

My code are mostly from the examples with slight modifications: 我的代码大部分来自示例,并进行了一些修改:

set up the layers: 设置图层:

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));

and the command handler to delete a column 以及删除列的命令处理程序

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;
    }
}

as said, this deletes the column content but leaves the header. 如前所述,这将删除列内容,但保留标题。 Can anyone tell me how I can also remove the column header? 谁能告诉我如何删除列标题?

Do you really want to delete a column or do you simply want to hide a column? 您是真的要删除列还是只想隐藏列? Because hiding would be much easier. 因为隐藏会容易得多。 Of course this depends on your use case and if your data model can be modified to really deleting a column. 当然,这取决于您的用例以及您的数据模型是否可以修改为真正删除列。

Nevertheless, the DefaultColumnHeaderDataProvider does not support dynamic adding or removing columns as it is based on an array. 但是,DefaultColumnHeaderDataProvider不支持动态添加或删除列,因为它基于数组。 For such an use case you need to provide a custom IDataProvider for the column header. 对于这种用例,您需要为列标题提供一个自定义IDataProvider。 The NatTable Examples application contains an example for that under Tutorial Examples -> Data -> DynamicColumnExample. NatTable示例应用程序在“教程示例->数据-> DynamicColumnExample”下包含一个示例。

You simply need to implement an IDataProvider that is based on a List rather than an array, so elements can be removed and the size modified. 您只需要实现一个基于List而不是数组的IDataProvider,因此可以删除元素并修改大小。

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

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