You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何扩展DefaultGlazedListsFilterStrategy忽略Nattable中null值的数值匹配?

解决Nattable数值过滤时忽略Null值的自定义策略

核心思路

直接在过滤逻辑中判定:如果目标数值列的单元格值为null,无论当前过滤条件是大于、小于还是等于某个值,都直接排除该行,不参与匹配判定,从根源避免null值干扰过滤结果,同时解决树形结构中子行误匹配导致父行强制显示的问题。

自定义FilterStrategy实现

继承DefaultGlazedListsFilterStrategy,重写过滤Predicate的创建逻辑,针对指定数值列添加null值排除规则:

import org.eclipse.nebula.widgets.nattable.data.IColumnAccessor;
import org.eclipse.nebula.widgets.nattable.filterrow.combobox.DefaultGlazedListsFilterStrategy;
import ca.odell.glazedlists.FilterList;
import ca.odell.glazedlists.matchers.Matcher;
import ca.odell.glazedlists.matchers.MatcherEditor;

public class NullExcludingGlazedListsFilterStrategy<T> extends DefaultGlazedListsFilterStrategy<T> {

    // 目标数值列的索引(比如Size in bytes列对应索引2)
    private final int targetNumericColumnIndex;

    public NullExcludingGlazedListsFilterStrategy(FilterList<T> filterList, IColumnAccessor<T> columnAccessor, int targetNumericColumnIndex) {
        super(filterList, columnAccessor);
        this.targetNumericColumnIndex = targetNumericColumnIndex;
    }

    @Override
    protected MatcherEditor<T> createMatcherEditor(int columnIndex, Object filterValue) {
        // 仅对目标数值列应用null排除逻辑
        if (columnIndex == this.targetNumericColumnIndex) {
            return new NullExcludingNumericMatcherEditor<>(columnIndex, filterValue, getColumnAccessor());
        }
        // 其他列保持默认过滤逻辑
        return super.createMatcherEditor(columnIndex, filterValue);
    }

    // 自定义MatcherEditor,专门处理数值列的null排除
    private static class NullExcludingNumericMatcherEditor<T> extends DefaultGlazedListsFilterStrategy.DefaultMatcherEditor<T> {

        public NullExcludingNumericMatcherEditor(int columnIndex, Object filterValue, IColumnAccessor<T> columnAccessor) {
            super(columnIndex, filterValue, columnAccessor);
        }

        @Override
        protected Matcher<T> createMatcher(Object filterValue) {
            return item -> {
                Object cellValue = getColumnAccessor().getDataValue(item, getColumnIndex());
                // 若单元格值为null,直接返回不匹配
                if (cellValue == null) {
                    return false;
                }
                // 非null值沿用原有比较逻辑
                return super.createMatcher(filterValue).matches(item);
            };
        }
    }
}

使用方式

初始化Nattable过滤策略时,替换为自定义策略并指定目标数值列索引:

// 假设已初始化FilterList和IColumnAccessor实例
int sizeColumnIndex = 2; // 对应Size in bytes列的索引
NullExcludingGlazedListsFilterStrategy<YourDataBean> filterStrategy = 
    new NullExcludingGlazedListsFilterStrategy<>(filterList, columnAccessor, sizeColumnIndex);

FilterRowDataLayer<YourDataBean> filterRowDataLayer = new FilterRowDataLayer<>(dataLayer, filterStrategy);
// 继续构建Nattable的其他层级...

原有方案失效原因

  • DefaultComparator遇到null时返回-1,会被过滤逻辑判定为匹配(比如过滤<1000时,null会被视为小于1000)。
  • nullsFirst/nullsLast仅负责排序时调整null的位置,不会改变过滤时的比较判定逻辑,无法在所有过滤条件下统一排除null值。
  • 树形结构中,子行误匹配会触发父行强制显示,自定义策略彻底排除null行后,子行不会触发父行的显示规则,过滤结果符合预期。

内容的提问来源于stack exchange,提问作者Sridhar Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 18:13:22