简体   繁体   English

如何在带有 Odata 的 aurelia slickgrid 中使用 gt/le 运算符

[英]How to use gt/le operator in aurelia slickgrid with Odata

I want to send my own operator in odata request and not use the aurelia slickgrid inbuilt "eq" operator.我想在 odata 请求中发送我自己的运算符,而不是使用 aurelia slickgrid 内置的“eq”运算符。

This is my column definition这是我的列定义

{
                id: 'LockoutEndDateUtc', name: 'Status', field: 'LockoutEndDateUtc', minWidth: 85, maxWidth: 95,
                type: FieldType.boolean,
                sortable: true,
                formatter: Formatters.multiple,
                params: { formatters: [this.StatusFormatter, Formatters.checkmark] },
                filterable: true,
                filter: {
                            collection: [ 
                                { value: 'le ' + (() => {const dt = new Date(); return dt.toISOString().split('.')[0] + "Z";})(), label: 'True' }, 
                                { value: 'gt ' + (() => {const dt = new Date(); return dt.toISOString().split('.')[0] + "Z";})(), label: 'False' }
                            ], //['', 'True', 'False'],
                            model: Filters.singleSelect,//multipleSelect//singleSelect,
                        }
            }

This is the UI这是用户界面在此处输入图像描述

This is how the request filter looks like..这就是请求过滤器的样子..

$filter=(LockoutEndDateUtc%20eq%20le%202022-06-28T12%3A59%3A25Z)

If i remove %20eq from the above request, everything else works.如果我从上述请求中删除 %20eq,其他一切正常。 So my question is how to i remove %20eq.所以我的问题是如何删除 %20eq。 Or how do i send my own gt, le in the request.或者我如何在请求中发送我自己的 gt, le。

You can't really do that on a boolean filter (you could however do it on a date filter with operator ) and I don't think I've added any ways to provide a custom filter search the way you want to do it, but since you're using OData, you have a bit more control and you could change the query string yourself.您不能在布尔过滤器上真正做到这一点(但是您可以在带有operator的日期过滤器上做到这一点)而且我认为我没有添加任何方法来按照您想要的方式提供自定义过滤器搜索,但由于您使用的是 OData,因此您有更多的控制权,您可以自己更改查询字符串。 To be clear, it's not at all recommended to change the OData query string, it's a last solution trick and at your own risk, but for your use case it might be the only way to achieve what you want.需要明确的是,根本不建议更改 OData 查询字符串,这是最后的解决方案,风险自负,但对于您的用例,它可能是实现您想要的唯一方法。

prepareGrid() {
    this.gridOptions = {
      // ...
      backendServiceApi: {
        service: new GridOdataService(),
        process: (query) => this.getCustomerApiCall(query),
      } as OdataServiceApi
    };
  }
}

getCustomerApiCall(query: string) {
  let finalQuery = query;
  // in your case, find the boolean value from the column and modify query

  // your logic to modify the query string
  // untested code, but it would probably look similar
  if (query.includes('LockoutEndDateUtc%20eq%20true')) {
    // calculate new date and replace boolean with new date
    finalQuery = query.replace('LockoutEndDateUtc%20eq%20true', 'LockoutEndDateUtc%20le%202022-06-28T12%3A59%3A25Z');
  }
  return finalQuery;
}

Another possible solution but requires a bit more work.另一种可能的解决方案,但需要更多的工作。

If I'd be using a regular grid, without backend service and without access to the query string, I would probably add an external drop down outside of the grid and also add the date column and then control filters in the grid by using dynamic filtering.如果我使用常规网格,没有后端服务并且无法访问查询字符串,我可能会在网格外部添加一个外部下拉列表,并添加日期列,然后使用动态过滤控制网格中的过滤器. You can see a demo at Example 23 , the principle is that you keep the column's real nature (date in your case) and filter it, if you want something like a "below today's date" then add an external way of filtering dynamically (a button or a drop down) and control the filter dynamically as shown below (from Example 23)您可以在Example 23看到一个演示,原则是您保持列的真实性质(在您的情况下为日期)并对其进行过滤,如果您想要“低于今天的日期”之类的内容,则添加一种外部动态过滤方式(a按钮或下拉菜单)并动态控制过滤器,如下所示(来自示例 23)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM