javaglazedlistsnattable

Delete multiple columns in NatTable


I'm using NatTable to display table data, the table can be sorted and filtered. Since the table is quite large I also used 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?


Solution

  • 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. For such an use case you need to provide a custom IDataProvider for the column header. The NatTable Examples application contains an example for that under Tutorial Examples -> Data -> 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.