过滤生成器对象;可以使用:条件字符串;参数;方法来构建; 条件字符串的过滤操作见Operations

Properties

Events

Constructor

FilterBuilder

FilterBuilder
(
  • filter
)
FilterBuilder

Parameters:

nametypeflagdescription
filter String | Function | Object

三种类型:

  1. {string}, 查询字符串
  2. {object}, 参数对象
  3. {function}, 过滤方法

Returns:

[FilterBuilder]

返回过滤生成器对象

Example:

    //定义数据
        var data = [
                {name: "roy",age: 30,role: "sa",project: "smartjs"},
                {name: "roy",age: 30,role: "coder",project: "smartdoc"},
                 {name: "coder1", age: 20, role: "coder", project: "smartjs"}
        ];

    //查询字符串,{age > @age}用{}包含的条件表示当@age为null的时候,忽略此条件
    var str = "{age > @age} and (role = @sa or role = @coder) and {project = @project}";

        //创建字符串过滤器
        var strFilter = st.filterBuilder(str);

        //生成过滤方法
        var fnFilterCoder = strFilter.buildFn({
            coder : 'coder',
        });

        //过滤所有coder
        var coders = data.filter(fnFilterCoder);

        expect(coders.length).toBe(2);
        expect(coders[0].name).toBe('roy');
        expect(coders[1].name).toBe('coder1');

        //再次生成smartjs项目年纪大于20的coder或sa
        var filterFn = strFilter.buildFn({
            age : 20,
            coder : 'coder',
            sa : 'sa',
            project : 'smartjs'
        });

        var member = data.filter(filterFn);
        expect(member.length).toBe(1);
        expect(member[0].name).toBe('roy');


        //创建过滤器
        var paramFilter = st.filterBuilder();

        //根据参数创建过滤方法
        var filterFn2 = paramFilter.buildFn({
            name : 'coder1'
        });

        var coder1 = data.filter(filterFn2);

        expect(coder1.length).toBe(1);
        expect(coder1[0].name).toBe('coder1');

Methods

buildCondition

buildCondition
(
  • params
)
Object

生成条件参数,当使用查询字符串进行构建过滤器时,根据传入的参数值生产最终的带关系和操作过滤参数

Parameters:

nametypeflagdescription
params Object

过滤的参数值

Returns:

[Object]

条件参数

Example:

               var str = "age > @age and (role = @sa or role = @coder) and project = @project";
            var filter = st.filterBuilder(str);

               //生成条件
               var conditions = filter.buildCondition({
                   age : 20,
                sa : 'sa',
                coder : 'coder',
                project : "smartjs"
            })

            log(conditions);

               // 生成的conditions对象
               // {"and":[
               //      {"field":"age","operation":">","param":20},
               //      {"or":[
               //         {"field":"role","operation":"=","param":"sa"},
               //         {"field":"role","operation":"=","param":"coder"}
               //      ]},
               //      {"field":"project","operation":"=","param":"smartjs"}
               // ]}

buildFn

buildFn
(
  • [params]
  • [mergeFilter]
)
Function

生成过滤方法

Parameters:

nametypeflagdescription
[params] Object optional

过滤的参数值

[mergeFilter] String | Function | Object optional

需要合并的过滤条件;合并全部都为and

Returns:

[Function]

过滤方法

Example:

    var data = [
        {name : 'sa1', role : 'sa', age : 33},
        {name : 'sa2', role : 'sa', age : 25}
    ];

    //创建role的过滤器
    var filter = st.filterBuilder("role = @role");

    //传入条件参数,并追加age的过滤
    var filterFn = filter.buildFn({role:"sa",age:30},"age > @age");

    var sa = data.filter(filterFn);
    expect(sa.length).toBe(1);
    expect(sa[0].name).toBe('sa1')
Top