2

如果使用多个分组,jqgrid 高级搜索可以生成搜索条件,如

{"groupOp":"AND","rules":[],"groups":[{"groupOp":"AND","rules":[{"field":"Nimi","op":"cn","data":"kk"},{"field":"Nimi","op":"cn","data":"kkk"}],"groups":[]}]}

尝试在 ASP .NET MVC2 中使用反序列化它

            var serializer = new JavaScriptSerializer();
            var filtersList = serializer.Deserialize<Filter>(_filters);


class Filter
{
    public GroupOp groupOp { get; set; }
    public List<Rule> rules { get; set; }
    public List<Filter> groups { get; set; }
}

class Rule
{
    public string field { get; set; }
    public Operations op { get; set; }
    public string data { get; set; }
}

enum GroupOp
{
    AND,
    OR
}

enum Operations
{
    eq, // "equal"
    ne, // "not equal"
    lt, // "less"
    le, // "less or equal"
    gt, // "greater"
    ge, // "greater or equal"
    bw, // "begins with"
    bn, // "does not begin with"
    @in, // "in"
    ni, // "not in"
    ew, // "ends with"
    en, // "does not end with"
    cn, // "contains"
    nc  // "does not contain"
}

返回空的 filtersList.rules 属性

如何从这些数据中得到正确的规则?

更新

筛选

    {"groupOp":"AND","rules":[{"field":"Nimi","op":"cn","data":""},
{"field":"Nimi","op":"cn","data":""},{"field":"Nimi","op":"cn","data":""}],
"groups":[{"groupOp":"AND","rules":[],"groups":[]},{"groupOp":"AND","rules":
[{"field":"Nimi","op":"cn","data":""}],"groups":[{"groupOp":"AND","rules":[],
"groups":[{"groupOp":"AND","rules":[],"groups":[]},{"groupOp":"AND","rules":[],"groups":[{"groupOp":"AND","rules":[],"groups":[{"groupOp":"AND","rules":[],
"groups":[{"groupOp":"AND","rules":[],"groups":[{"groupOp":"AND","rules":[],"groups":[]},{"groupOp":"AND","rules":[],"groups":[]}]}]}]}]},{"groupOp":"AND","rules":[{"field":"Nimi","op":"cn","data":""}],"groups":[{"groupOp":"AND","rules":[{"field":"Nimi","op":"cn","data":""},{"field":"Nimi","op":"cn","data":""}],
"groups":[]}]},{"groupOp":"AND","rules":[],"groups":[]},{"groupOp":"AND","rules":[],"groups":[]},{"groupOp":"AND","rules":[],"groups":[]},{"groupOp":"AND","rules":[],"groups":[]}]}]}]}

在引用的答案中使用更新部分的代码仍然会产生无效的 where

    ((klient.Nimi ILIKE ('%' || E'' || '%') ESCAPE '!')AND(klient.Nimi ILIKE
 ('%' || E'' || '%') ESCAPE '!')AND(klient.Nimi ILIKE ('%' || E'' || '%') ESCAPE '!'))AND
((((klient.Nimi ILIKE ('%' || E'' || '%') ESCAPE '!'))AND(((((())))AND(((klient.Nimi ILIKE ('%' || E'' || '%') ESCAPE '!'))AND(((klient.Nimi ILIKE ('%' || E'' || '%') ESCAPE '!')
AND(klient.Nimi ILIKE ('%' || E'' || '%') ESCAPE '!'))))))))
4

1 回答 1

1

我看代码没问题

const string filters = "{\"groupOp\":\"AND\",\"rules\":[]," +
    "\"groups\":[{\"groupOp\":\"AND\",\"rules\":[" +
        "{\"field\":\"Nimi\",\"op\":\"cn\",\"data\":\"kk\"}," +
        "{\"field\":\"Nimi\",\"op\":\"cn\",\"data\":\"kkk\"}],\"groups\":[]}]}";

var serializer = new JavaScriptSerializer();
var filtersList = serializer.Deserialize<Filter>(filters);

产生filtersListwherefiltersList.rules是空的列表。它对应于您拥有的输入数据。另一方面,该filtersList.groups部分不是空的。从上面的filtersList代码将产生Filter相当于以下直接初始化的对象:

var filtersList = new Filter {
    groupOp = GroupOp.AND,
    rules = new List<Rule>(0),
    groups = new List<Filter> {
        new Filter {
            groupOp = GroupOp.AND,
            rules = new List<Rule> {
                new Rule {field = "Nimi", op = Operations.cn, data = "kk"},
                new Rule {field = "Nimi", op = Operations.cn, data = "kkk"}
            },
            groups = new List<Filter>(0)
        }
    }
};

如果一个人从一个人那里解析信息,filtersList最后仍然可以得到WHERE如下语句

(Nimi LIKE '%kk%') AND (Nimi LIKE '%kkk%')

例如,请参阅答案的更新部分的代码。确切地说,答案产生的代码((Nimi LIKE '%kk%')AND(Nimi LIKE '%kkk%'))实际上是相同的。

于 2012-04-07T20:54:50.850 回答