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

求@progress/kendo-data-query中FilterDescriptor的operator函数签名

问题描述

根据官方文档说明,FilterDescriptor的operator属性可以是函数类型。

filterDescriptor.d.ts

/**
 * 基础过滤表达式,通常作为CompositeFilterDescriptor的一部分
 *
 * 更多信息请参考`filterBy`方法
 */
export interface FilterDescriptor {
    /**
     * 过滤运算符要作用到的数据项字段
     */
    field?: string | Function;
    /**
     * 过滤运算符(比较逻辑)
     *
     * 支持的运算符如下:
     * * `"eq"` (等于)
     * * `"neq"` (不等于)
     * * `"isnull"` (等于null)
     * * `"isnotnull"` (不等于null)
     * * `"lt"` (小于)
     * * `"lte"` (小于等于)
     * * `"gt"` (大于)
     * * `"gte"` (大于等于)
     *
     * 以下运算符仅支持字符串类型字段:
     * * `"startswith"` (开头匹配)
     * * `"endswith"` (结尾匹配)
     * * `"contains"` (包含)
     * * `"doesnotcontain"` (不包含)
     * * `"isempty"` (为空)
     * * `"isnotempty"` (不为空)
     */
    operator: string | Function;
    /**
     * 字段要比较的值,必须和字段类型一致
     */
    value?: any;
    /**
     * 字符串比较时是否忽略大小写
     */
    ignoreCase?: boolean;
}

目前全网暂未查询到FilterDescriptor.operator对应的函数签名规范,且Kendo不开源无法直接查看源码确认,咨询该函数的具体签名定义。


解答

可以通过Kendo公开的filterBy方法运行逻辑,直接推导出该函数的标准签名:

/**
 * 自定义过滤运算符函数
 * @param fieldValue 待过滤数据项对应字段的实际值
 * @param filterValue FilterDescriptor中配置的value属性值
 * @param ignoreCase FilterDescriptor中配置的ignoreCase属性,仅字符串比较场景生效
 * @returns 布尔值,true表示当前数据项符合过滤条件,false表示不符合
 */
type FilterOperatorFunction = (fieldValue: any, filterValue: any, ignoreCase?: boolean) => boolean;

实际使用示例

import { FilterDescriptor, filterBy } from '@progress/kendo-data-query';

const demoData = [
  { name: '张三', age: 25, department: '技术部' },
  { name: '李四', age: 30, department: '产品部' },
  { name: '王老五', age: 35, department: '技术部' }
];

// 自定义过滤规则:筛选技术部且年龄大于28岁的员工
const customFilter: FilterDescriptor = {
  field: item => item, // field配置为函数直接返回整个数据项,方便operator中访问所有字段
  operator: (item, filterMinAge) => {
    return item.department === '技术部' && item.age > filterMinAge;
  },
  value: 28
};

const filteredData = filterBy(demoData, customFilter);
// 输出结果:[{ name: '王老五', age: 35, department: '技术部' }]

注意事项

  • 配置函数类型的operator后,Kendo内置的所有默认比较逻辑都会失效,完全以自定义函数的返回值作为过滤判断依据
  • 不需要用到ignoreCase参数时可以直接省略,TS类型校验不会报错
  • 如果需要在过滤逻辑中访问数据项的所有字段,直接把field属性设置为返回当前数据项的函数即可,不需要单独拼接多个FilterDescriptor

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 00:15:03