NatTable技术实现:基于前置选择动态变更筛选行下拉框选项
NatTable筛选行下拉框联动实现方案
要实现筛选行中下拉框的动态联动,核心是监听第一个下拉框的编辑事件,在值变更时触发第二个下拉框的选项刷新,并在第二个下拉框激活时根据当前依赖值动态生成选项列表。以下是具体实现步骤和代码示例:
1. 监听筛选行的编辑事件
当第一个筛选下拉框的值变更时,通知NatTable刷新第二个筛选单元格,确保后续激活第二个下拉框时能获取最新的依赖值。
// 获取你的FilterRowDataLayer实例 FilterRowDataLayer filterRowLayer = ...; // 添加层监听器,捕获单元格编辑完成事件 filterRowLayer.addLayerListener(new ILayerListener() { @Override public void handleLayerEvent(ILayerEvent event) { if (event instanceof CellEditorClosedEvent) { CellEditorClosedEvent closeEvent = (CellEditorClosedEvent) event; // 筛选行通常是第0行,判断是否是第一个筛选列(索引0)的编辑完成 if (closeEvent.getRowPosition() == 0 && closeEvent.getColumnPosition() == 0) { // 刷新第二个筛选列(索引1)的单元格区域,触发编辑器重新初始化 filterRowLayer.fireLayerEvent(new VisualRefreshEvent( filterRowLayer, new Rectangle(1, 0, 1, 1) )); } } } });
2. 自定义动态联动的下拉框编辑器
创建继承自ComboBoxCellEditor的自定义编辑器,在激活时根据第一个筛选列的当前值动态生成选项列表。
class DependentComboBoxCellEditor extends ComboBoxCellEditor { private final NatTable natTable; private final int dependentColumnIndex; public DependentComboBoxCellEditor(NatTable natTable, int dependentColumnIndex) { super(new ArrayList<>()); // 初始空列表,激活时动态填充 this.natTable = natTable; this.dependentColumnIndex = dependentColumnIndex; } @Override public void activateCell(NatTable natTable, Rectangle cellBounds, ILayerCell cell, Event event) { // 获取第一个筛选列的当前筛选值 FilterRowDataLayer filterRowLayer = (FilterRowDataLayer) natTable.getLayerByClass(FilterRowDataLayer.class); Object dependentValue = filterRowLayer.getFilterValue(dependentColumnIndex); // 根据依赖值生成动态选项(替换为你的业务逻辑) List<String> dynamicOptions = getDynamicSopOptions(dependentValue); Collections.sort(dynamicOptions); // 更新下拉框选项 this.setItems(dynamicOptions); // 调用父类方法激活编辑器 super.activateCell(natTable, cellBounds, cell, event); } // 业务逻辑:根据第一个下拉框的值返回对应的SOP列表 private List<String> getDynamicSopOptions(Object dependentValue) { List<String> options = new ArrayList<>(); if (dependentValue == null) { return EZPDContants.SOP_LIST; // 默认显示全部选项 } // 示例:根据不同的状态值返回对应SOP选项 switch (dependentValue.toString()) { case "ACTIVE": options.add("SOP_ACTIVE_01"); options.add("SOP_ACTIVE_02"); break; case "INACTIVE": options.add("SOP_INACTIVE_01"); break; default: options.addAll(EZPDContants.SOP_LIST); } return options; } }
3. 注册编辑器到配置注册表
替换原有固定编辑器的注册逻辑,将自定义动态编辑器注册到第二个筛选列。
// 注册第一个筛选列的状态下拉框编辑器 Collections.sort(EZPDContants.STATUS_LIST); ComboBoxCellEditor statusEditor = new ComboBoxCellEditor(EZPDContants.STATUS_LIST); configRegistry.registerConfigAttribute( EditConfigAttributes.CELL_EDITOR, statusEditor, DisplayMode.NORMAL, FilterRowDataLayer.FILTER_ROW_COLUMN_LABEL_PREFIX + 0 ); // 注册第二个筛选列的动态联动SOP下拉框编辑器 DependentComboBoxCellEditor sopEditor = new DependentComboBoxCellEditor(natTable, 0); configRegistry.registerConfigAttribute( EditConfigAttributes.CELL_EDITOR, sopEditor, DisplayMode.NORMAL, FilterRowDataLayer.FILTER_ROW_COLUMN_LABEL_PREFIX + 1 );
关键说明
FilterRowDataLayer.getFilterValue(int columnIndex):用于获取指定筛选列的当前值,是实现联动的核心数据来源。VisualRefreshEvent:触发指定单元格区域的刷新,确保第二个下拉框激活时重新加载动态选项。- 自定义编辑器的
activateCell方法:每次激活下拉框时都会重新计算选项列表,保证选项与当前依赖值一致。
内容的提问来源于stack exchange,提问作者mahesh bhagirath
相关产品推荐
相关产品推荐

