删除NatTable中的多个列

时间:2015-07-06 14:28:30

标签: java glazedlists nattable

我使用NatTable显示表格数据,可以对表格进行排序和过滤。由于表格非常大,我还使用了GlazedList。我需要能够在排序和过滤后删除列。我试过,我只能删除表的内容,但标题仍然存在。列标题嵌套在很多层中,我不知道我可能会影响或触发刷新。

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

设置图层:

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

如上所述,这将删除列内容但保留标题。任何人都可以告诉我如何删除列标题吗?

1 个答案:

答案 0 :(得分:0)

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

尽管如此,DefaultColumnHeaderDataProvider不支持动态添加或删除列,因为它基于数组。对于这样的用例,您需要为列标题提供自定义IDataProvider。 NatTable示例应用程序在教程示例下包含了一个示例 - &gt;数据 - &gt; DynamicColumnExample。

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