Angular 2 Kendo toOdataString返回空白字符串

时间:2017-12-17 16:25:33

标签: javascript angular kendo-ui odata

如果我传递以下对象

{  
   "logic":"and",
   "filters":[  
      {  
         "logic":"or",
         "filters":[  
            {  
               "field":"text",
               "operator":"neq",
               "value":""
            }
         ]
      },
      {  
         "logic":"or",
         "filters":[  
            {  
               "field":"userActivityVetSpecializations.vetSpecializationId",
               "operator":"neq",
               "value":"-1"
            }
         ]
      }
   ]
}

到kendo angular函数toODataString,我收到一个空字符串响应。

任何建议让kendo返回格式正确的odata字符串?

1 个答案:

答案 0 :(得分:1)

看来旧的kendo javascript核心代码处理得当,所以我把它转换成了打字稿。

 toODataFilter(filter) {
var result = [],
  logic = filter.logic || "and",
  idx,
  length,
  field,
  type,
  format,
  operator,
  value,
  ignoreCase,
  filters = filter.filters;

for (idx = 0, length = filters.length; idx < length; idx++) {
  filter = filters[idx];
  field = filter.field;
  value = filter.value;
  operator = filter.operator;

  if (filter.filters) {
    filter = this.toODataFilter(filter);
  } else {
    ignoreCase = filter.ignoreCase;
    field = field.replace(/\./g, "/");
    filter = this.odataFilters[operator];


    if (operator === "isnull" || operator === "isnotnull") {
      filter = this.String.format("{0} {1} null", field, filter);
    } else if (operator === "isempty" || operator === "isnotempty") {
      filter = this.String.format("{0} {1} ''", field, filter);
    } else if (filter && value !== undefined) {
      type = typeof value;
      if (type === "string") {
        format = "'{1}'";
        value = value.replace(/'/g, "''");

        if (ignoreCase === true) {
          field = "tolower(" + field + ")";
        }

      } else if (type === "date") {
          format = "{1:yyyy-MM-ddTHH:mm:ss+00:00}";
        // TODO **** Time zone
        //value = kendo.timezone.apply(value, 'Etc/UTC');

      } else {
        format = "{1}";
      }

      if (filter.length > 3) {
        if (filter !== "substringof") {
          format = "{0}({2}," + format + ")";
        } else {
          format = "{0}(" + format + ",{2})";
          if (operator === "doesnotcontain") {           
              format = "{0}({2},'{1}') eq -1";
              filter = "indexof";              
          }
        }
      } else {
        format = "{2} {0} " + format;
      }

      filter = this.String.format(format, filter, value, field);
    }
  }

  result.push(filter);
}

filter = result.join(" " + logic + " ");

if (result.length > 1) {
  filter = "(" + filter + ")";
}

return filter;

}

物件

  odataFilters = {
eq: "eq",
neq: "ne",
gt: "gt",
gte: "ge",
lt: "lt",
lte: "le",
contains: "contains",
doesnotcontain: "substringof",
endswith: "endswith",
startswith: "startswith",
isnull: "eq",
isnotnull: "ne",
isempty: "eq",
isnotempty: "ne"};

String: any = {};

将它放在构造函数

      this.String.format = function (format) {
    var args = Array.prototype.slice.call(arguments, 1);
    return format.replace(/{(\d+)}/g, function (match, number) {
      return typeof args[number] != 'undefined'
        ? args[number]
        : match
        ;
    });
  };

这段代码似乎对我来说很好。有一个我不需要的日期案例,所以我对它进行了评论。