Appframe Knowledge Base


1 hits
0

How to define a filter object in JavaScript

// For the filter to be compatible with the filter builder it must be 
// an or-group containing and-groups containing filter expressions.
// If you do not need to support opening the defined filter in the filter builder,
// you can use any structure and it will work when applied to a data object.

var vFilterObject = {
    type: "group",
    mode: "and" || "or",
    items: [
        { /* Groups can be nested in groups to get all possible "and/or" combinations */
            type: "group",
            mode: "and" || "or",
            items: [/* and so on */]
        },
        { /* Expressions having one of these operators must have a single value and its corresponding valueType */
            type: "expression",
            column: "MyColumnName",
            operator:   "equals"
                    ||  "notequals"
                    ||  "greaterthan"
                    ||  "greaterthanorequal"
                    ||  "lessthan"
                    ||  "lessthanorequal"
                    ||  "beginswith"
                    ||  "notbeginswith"
                    ||  "endswith"
                    ||  "notendswith"
                    ||  "contains"
                    ||  "notcontains"
                    ||  "like"
                    ||  "notlike"
                    ||  "dateequals"
                    ||  "timeequals"
                    ||  "timebefore"
                    ||  "timeafter",
            value: "Some value" || 12345 || new Date(),
            valueType: "string" || "number" || "datetime" || "uniqueidentifier"
        },
        { /* Expressions having one of these operators must have value = null and valueType = "null" */
            type: "expression",
            column: "MyColumnName",
            operator:   "isnull"
                    ||  "isnotnull"
                    ||  "istrue"
                    ||  "isfalse"
                    ||  "isblank"
                    ||  "isnotblank",
            value: null,
            valueType: "null"
        },
        { /* Expressions having one of these operators must have an array of two values of a common valueType */
            type: "expression",
            column: "MyColumnName",
            operator:   "between"
                    ||  "notbetween"
                    ||  "timebetween"
                    ||  "timenotbetween",
            value: [
                "a" || 1 || new Date(Date.now() - 1000*60*60*24),
                "z" || 1000 || new Date()
            ],
            valueType: "string" || "number" || "datetime" || "uniqueidentifier"
        },
        { /* Expressions having one of these operators can have one of more values in an array of a common valueType */
            type: "expression",
            column: "MyColumnName",
            operator:   "inlist"
                    ||  "notinlist",
            value: [
                "a" || 1 || new Date(Date.now() - 1000*60*60*24*1),
                "b" || 2 || new Date(Date.now() - 1000*60*60*24*2),
                "c" || 3 || new Date(Date.now() - 1000*60*60*24*3),
                "d" || 4 || new Date(Date.now() - 1000*60*60*24*4),
                "e" || 5 || new Date(Date.now() - 1000*60*60*24*5),
            ],
            valueType: "string" || "number" || "datetime" || "uniqueidentifier"
        }
    ]
};

// NOTE: All the pipes ( | ), are just there to indicate your possibilities,
// and should not actually be present in your filters
        
// If you want to validate the filter, turn it into SQL client-side, or apply it on an array, you can use the af.Filter class
var vFilterInstance = new af.Filter(vFilterObject);

vFilterInstance.toSQL(); // returns the filter as an SQL

var vArrayOfRows = [{ MyColumnName: "Some value" }];
vFilterInstance.filter(vArrayOfRows); // would filter the array using the defined filter

components web · Perma link post comment Posted by: Jan Henrik Høiland Meling (18-okt-2013)