Angular 2 Kendo toOdataString返回空白字符串

问题描述 投票:1回答:1

如果我传递以下对象

{  
   "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字符串?

javascript angular kendo-ui odata
1个回答
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
        ;
    });
  };

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

© www.soinside.com 2019 - 2024. All rights reserved.