how to deserialize jqgrid multiple group search criteria in asp .net mvc2

穿精又带淫゛_ 提交于 2019-12-11 01:15:44

问题


If multiple grouping is used, jqgrid advanced search can generate search criteria like

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

Trying to de-serialize it in ASP .NET MVC2 using

            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"
}

returns empty filtersList.rules property

How to get correct rules from this data ?

update

filter

    {"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":[]}]}]}]}

using code from updated part in referenced answer still generates invalid 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 '!'))))))))

回答1:


I see no problem that the code

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);

produce the filtersList where filtersList.rules is empty list. It corresponds to the input data which you has. On the other side the filtersList.groups part is not empty. The filtersList from the above code will produce the Filter object which equivalent to the following direct initializing:

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)
        }
    }
};

If one parse the information from the filtersList one can get at the end still the WHERE statement like the following

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

see the code of UPDATED part of the answer for example. To be exactly the code from the answer produces ((Nimi LIKE '%kk%')AND(Nimi LIKE '%kkk%')) which is practically the same.



来源:https://stackoverflow.com/questions/10053415/how-to-deserialize-jqgrid-multiple-group-search-criteria-in-asp-net-mvc2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!