NatTable: Dynamically Changing Filter Row Combo Filter Based on Previous Selection

65 Views Asked by At

I am working with NatTable in my Java application, and I have a specific requirement related to the filter row and combo filters. In my NatTable, I have a filter row with combo box filters, and I would like to dynamically change the options available in one combo box based on the selection made in a previous combo box in the same row.

For example, if the user selects an option in the first combo box, I want to update the options available in the second combo box in the same row dynamically.

I am currently using EditConfigAttributes.CELL_EDITOR to handle cell combo values. However, I'm unsure about the best way to dynamically update the options of the combo box based on the selection in a previous combo box.

Could someone provide guidance or an example code snippet on how to achieve this in NatTable? Any help or insights would be greatly appreciated!

Collections.sort(EZPDContants.STATUS_LIST);
ComboBoxCellEditor comboBoxCellEditor = new ComboBoxCellEditor(MyContants.STATUS_LIST);
configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITOR, comboBoxCellEditor, DisplayMode.NORMAL,
        FilterRowDataLayer.FILTER_ROW_COLUMN_LABEL_PREFIX + 0);

Collections.sort(EZPDContants.SOP_LIST);
ComboBoxCellEditor attributeValue = new ComboBoxCellEditor(MyContants.SOP_LIST);
configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITOR, attributeValue, DisplayMode.NORMAL,
        FilterRowDataLayer.FILTER_ROW_COLUMN_LABEL_PREFIX + 1);

Thank you in advance.

2

There are 2 best solutions below

2
On BEST ANSWER

Instead of passing a fixed list you can create the ComboBoxCellEditor with a IComboBoxDataProvider. That one somehow needs to know the context to determine which values to return.

See https://eclipse.dev/nattable/documentation.php?page=faq for an example.

0
On

In my implementation of IComboBoxDataProvider#getValues, I've successfully achieved dynamic filter changes. I'm utilizing a FilterList<MyData> within the method to extract distinct values based on the provided column index. The following code snippet illustrates this approach:


    @Override
    public List<?> getValues(int columnIndex, int rowIndex) {
        // Assuming you have a FilterList<MyData> named filterList
        FilterList<MyData> filterList = ...;
    
        // Ensure filterList is not null
        if (filterList == null) {
            return Collections.emptyList();
        }
    
        // Dynamically provide distinct values for the specified column
        switch (columnIndex) {
            case 0:
                // Extract distinct values from the specified column using Java streams
                return filterList.stream()
                    .map(MyData::getOk)
                    .distinct()
                    .sorted()
                    .collect(Collectors.toList());
            default:
                break;
        }
    
        return Collections.emptyList();
    }