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

Aurelia-SlickGrid不区分大小写搜索ODATA API问题求助

解决Aurelia-SlickGrid结合ODATA API的大小写敏感搜索问题

你遇到的是ODATA contains函数默认大小写敏感导致的搜索问题,后端不支持substringof,可以通过Aurelia-SlickGrid的过滤器配置解决,以下是两种可行方案:

方案一:单个列自定义过滤器格式化逻辑

在Name列的定义中添加filter配置,自定义filterFormatter生成不区分大小写的ODATA过滤表达式:

this.customerColumnDefinition = [
    { 
        id: "Name", name: "Name", field: "Name", sortable: true, type: FieldType.string, filterable: true, minWidth: 100,
        filter: {
            model: Filters.input, // 保持输入框类型的过滤器
            filterFormatter: (filterItem: FilterItem) => {
                if (filterItem?.value) {
                    // 使用ODATA的toUpper函数实现大小写不敏感匹配
                    return `toUpper(${filterItem.field}) contains toUpper('${filterItem.value}')`;
                }
                return '';
            }
        },
        onCellClick: (e: Event, args: OnEventArgs): void => { this.router.navigateToRoute("customer", { businessUnitId: args.dataContext.Id }); }
    },
    // 其他列定义...
];

注意:如果你的ODATA版本是v3,函数名可能是toupper(全小写),请根据后端实际支持的函数名调整。

方案二:全局统一配置ODATA过滤器

如果需要所有字符串列的搜索都自动支持大小写不敏感,可以自定义ODataBackendService并重写过滤条件的生成逻辑:

  1. 创建自定义服务类:
import { ODataBackendService, FilterItem } from 'aurelia-slickgrid';

export class CustomODataBackendService extends ODataBackendService {
  buildFilterQuery(filterCollection: FilterItem[]): string {
    let filterQuery = super.buildFilterQuery(filterCollection);
    
    // 批量替换所有contains表达式为大小写不敏感版本
    filterQuery = filterQuery.replace(
      /contains\(([^,]+),\s*'([^']+)'\)/g,
      (match, field, value) => `toUpper(${field}) contains toUpper('${value}')`
    );
    
    return filterQuery;
  }
}
  1. 在Aurelia配置中注册自定义服务,替换默认的ODataBackendService:
import { PLATFORM } from 'aurelia-pal';
import { ODataBackendService } from 'aurelia-slickgrid';
import { CustomODataBackendService } from './services/custom-odata-backend-service'; // 替换为你的实际文件路径

export function configure(aurelia) {
  aurelia.use
    .standardConfiguration()
    .plugin(PLATFORM.moduleName('aurelia-slickgrid'), config => {
      // 注册自定义ODATA后端服务
      config.registerBackendService(ODataBackendService, CustomODataBackendService);
    });

  // 其他项目配置...
}

这样所有通过Aurelia-SlickGrid生成的ODATA contains过滤条件都会自动转换为大小写不敏感的形式,无需逐个列单独配置。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 21:40:39