使用toLowerCase过滤一组值的导航属性

时间:2014-03-30 20:01:53

标签: javascript odata jaydata

我有这个JayData查询(可以在JSLQ playground处查看):

northwind.Products
  .filter( function( product ) {
    return  product.ProductName.toLowerCase() in ['tofu'];
  } )
  .toArray( function( products ) {
    console.dir( products );
  } );

由于某些未知原因,此查询会生成错误的过滤器表达式: http://services.odata.org/Northwind/Northwind.svc/Products?$filter=(tolowerProductName%20eq%20%27tofu%27)&$callback=parent.handleJSONP_21&$format=json

我用上面的粗体突出显示了错误的参数。

所以你可以看到我们有tolowerProductName,但它应该是tolower(ProductName),所以正确的过滤器查询应如下所示: http://services.odata.org/Northwind/Northwind.svc/Products?$filter=%28tolower%28ProductName%29%20eq%20%27tofu%27%29&$callback=parent.handleJSONP_21&$format=json

有人知道解决方法吗?

更新: 只有当我在[array]"中使用toLowerCase时才会出现问题,所以例如这个查询工作得很好:

northwind.Products
  .filter( function( product ) {
    return  product.ProductName in ['Tofu'];
  } )
  .toArray( function( products ) {
    console.dir( products );
  } );

使用toLowerCase的查询也正常工作:

northwind.Products
  .filter( function( product ) {
    return  product.ProductName.toLowerCase() == 'Tofu';
  } )
  .toArray( function( products ) {
    console.dir( products );
  } );

谢谢!

2 个答案:

答案 0 :(得分:1)

您是否尝试将该表达式设置为变量,然后在['豆腐']中检查该表达式?

答案 1 :(得分:0)

我发现错误的过滤器表达式产生了这行代码(用于'在'运算符中查询):

context.data = temp + context.data.replace(/\(/g, '').replace(/\)/g, '');

Line 1542 in oDataProvider.js

我用这段代码替换了它:

context.data = temp + context.data;

现在一切都按预期工作了。也许我已经介绍了此修复程序的其他问题,但至少它可以按预期的方式运行我的查询。

因此,VisitSimpleBinaryExpression函数现在看起来像这样:

VisitSimpleBinaryExpression: function (expression, context) {
        context.data += "(";
        //TODO refactor!!!
        if (expression.nodeType == "in") {
            Guard.requireType("expression.right", expression.type, $data.Expressions.ConstantExpression);
            var paramValue = expression.right.value;
            if (!paramValue instanceof Array) { Guard.raise(new Exception("Right to the 'in' operator must be an array value")); }
            var result = null;
            var orResolution = { mapTo: "or", dataType: "boolean", name: "or" };
            var eqResolution = { mapTo: "eq", dataType: "boolean", name: "equal" };

            paramValue.forEach(function (item) {
                var idValue = item;
                var idCheck = Container.createSimpleBinaryExpression(expression.left, idValue,
                    $data.Expressions.ExpressionType.Equal, "==", "boolean", eqResolution);
                if (result) {
                    result = Container.createSimpleBinaryExpression(result, idCheck,
                    $data.Expressions.ExpressionType.Or, "||", "boolean", orResolution);
                } else {
                    result = idCheck;
                };
            });
            var temp = context.data;
            context.data = '';
            this.Visit(result, context);
            //context.data = temp + context.data.replace(/\(/g, '').replace(/\)/g, '');
            context.data = temp + context.data;
        } else {
            this.Visit(expression.left, context);
            context.data += " ";
            context.data += expression.resolution.mapTo;
            context.data += " ";
            this.Visit(expression.right, context);
        };
        context.data += ")";

    },