简体   繁体   中英

Gridx Server-side filtering - reference? example?

I have successfully created a:

  • Gridx using JSONStore
  • Server-side paging using 'Range : items=0-99' header
  • Server-side sorting using 'sort(+name)'

... but after much effort and searching I haven't been able to setup the Filterbar module to perform Server-side filtering.

I have used 'filterSetupQuery' to pull information out of JSON (on the client) and append to the get URL for very basic filters, but with complicated filters it seems to make more sense to process the JSON in the Java based controller on the server.

What would this Java class and FlexJson deserializer even look like? Does someone have a reference implementation of Server-side filtering, or even an example to show how to deserialize this object in Java?

Here is a simple JSON object that is sent back to the controller:

{  "op":"and",
 "data":[{  "op":"or",
          "data":[{"op":"contain","data":[{"op":"string","data":"1","isCol":true},
                                          {"op":"string","data":"john"}]},
                  {"op":"contain","data":[{"op":"string","data":"2","isCol":true},
                                          {"op":"string","data":"john"}]},
                  {"op":"contain","data":[{"op":"string","data":"3","isCol":true},
                                          {"op":"string","data":"john"}]},
                  {"op":"contain","data":[{"op":"string","data":"4","isCol":true},
                                          {"op":"string","data":"john"}]}
]}]}

Any help is greatly appreciated! Thank you Chris

You can check gridx source code.. Under \\tests\\test_grid_filter_serverside.html

filterSetupFilterQuery: function(expr){
            var toExpr = function(expr){
                if(!expr){ return ""; }
                if(typeof expr.data != "object"){
                    if(expr.isCol){
                        return "column(\"" + expr.data + "\")";
                    }
                    return "\"" + expr.data + "\"";
                }else{
                    var exprs = [];
                    for(var i in expr.data){
                        exprs.push(toExpr(expr.data[i]));
                    }
                    var op = expr.op;
                    if(expr.op == "and"){
                        op = "logicand";
                    }
                    if(expr.op == "or"){
                        op = "logicor";
                    }
                    return op + "(" + exprs.join(",") + ")";
                }
            };
            console.log("expr is: ", expr);     

            var newExpr = toExpr(expr);
            if(newExpr){ newExpr += ";"}
            console.log("expr is: ", newExpr);      
            return {query: newExpr};    
        },

, the function above outputs something like ..

logicor(logicor(contain(column("1"),"drawal"),contain(column("2"),"drawal"),contain(column("3"),"drawal"),contain(column("4"),"drawal"),contain(column("5"),"drawal"),contain(column("6"),"drawal"),contain(column("7"),"drawal")));

I was able to change the function to output sql as below

var toExpr = function(expr) {
                                    if (!expr) {
                                        return "";
                                    }
                                    if ( typeof expr.data != "object") {
                                        if (expr.isCol) {
                                            return cols.item(expr.data);
                                            // return "column(\"" + expr.data + "\")";
                                        }
                                        return "\"" + expr.data + "\"";
                                    } else {
                                        var exprs = [];
                                        for (var i in expr.data) {
                                            exprs.push(toExpr(expr.data[i]));
                                        }
                                        var op = expr.op;                                           

                                        if (op == 'not') {
                                            return "(" + exprs[0].replace(/=/, '<>').replace(/like/, ' not like ') + ")";
                                        }

                                        if (op == 'contain') {
                                            return "(" + exprs[0] + ' like ' + exprs[1].replace(/^"/, '"%').replace(/"$/, '%"') + ")";
                                        }

                                        if (op == 'startWith') {
                                            return "(" + exprs[0] + ' like ' + exprs[1].replace(/^"/, '"%') + ")";
                                        }

                                        if (op == 'endWith') {
                                            return "(" + exprs[0] + ' like ' + exprs[1].replace(/"$/, '"%') + ")";
                                        }

                                        return "(" + exprs.join(" " + op.replace(/equal/, '=') + " ") + ")";                                            
                                    }
                                };

                                var newExpr = toExpr(expr);
                                if (newExpr) {
                                    newExpr += ";";
                                }
                                console.log("expr is: ", newExpr);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM