Angular2的KendoUI的documentation提到toODataString方法将提供的状态对象转换为oData v4兼容字符串。
所述状态对象的结构定义为here。
我已经为Angular2网格设置了一个KendoUI,其数据绑定和服务器端分页如here所述,它运行正常。
所以现在我想在该状态下添加一个过滤器对象,并且我使用以下代码来执行此操作:
private filter: CompositeFilterDescriptor;
public applyFilters(): void {
this.filter = {
logic: "and",
filters: []
};
this.skip = 0;
this.sort = [];
if (this.customerNameFilter) {
let fd: FilterDescriptor = {
field: "name",
operator: "contains",
value: this.customerNameFilter,
ignoreCase: true
}
this.filter.filters.push(fd);
}
this.service.query({ skip: this.skip, take: this.pageSize, filter: this.filter });
}
然后,service.query方法调用toODataString方法,并返回以下查询字符串(请注意双符号,就像它尝试插入评估为null的内容一样):
$跳过= 0&安培; $顶部= 15&安培;&安培; $计数=真
以下是调试器的屏幕截图,显示了传递给toODataString方法的filterDescriptor实例的结构。请告诉我这里做错了什么?
答案 0 :(得分:1)
原来它在toODataString方法的当前测试版中不受支持。我查看了它的源代码,发现没有处理传递给它的状态的filter属性。
所以我将自己的filterSerializer附加到查询字符串:
private fetch(tableName: string, state: any, filter: CompositeFilterDescriptor): Observable<GridDataResult> {
const queryStr = `${toODataString(state) + this.serializeFilter(filter)}&$count=true`;
return this.http
.get(`${this.BASE_URL}${tableName}?${queryStr}`)
.map(response => response.json())
.map(response => (<GridDataResult>{
data: response.value,
total: parseInt(response["@odata.count"], 10)
}));
}
private serializeFilter(filter: CompositeFilterDescriptor): string {
if (filter === null)
return "";
var filterString = "&$filter=";
var filters = filter.filters.map(function (fd: FilterDescriptor) {
switch (fd.operator) {
case "contains":
return "contains(" + fd.field + ", '" + fd.value + "')";
case "eq":
return fd.field + " eq '" + fd.value + "'";
}
})
.join(" " + filter.logic + " ");
filterString += filters;
return filterString;
}