iRSF快速简单易用的实现列表、排序、过滤功能

 IRSF 是由javascript编写,iRSF快速简单易用的实现列表、排序、过滤功能(该三种操作以下简称为 RSF )。

iRSF由三个类组成。

iRSFSource 数据源
iRSFFilter 过滤器
iRSFSorter 排序器  

iRSF 使用:

iRsf = new iRSF();
iRsf.draw = function(data){
//展现列表,data的结构为{property:[{data1},{data2}]},* property 可以自定义,由iRSFSource 指定。
};
//指定数据源
iRsf.setSource({
src:{items:[{id:1121,name:"dfsa"},{id:1122,name:"dfsa"}]},
property:"items"
});

//添加过滤器
iRsf.addFilter("id",function(row){
return row.id==1121;
});

//设置排序
iRsf.setSort(function(a,b){
return a.id-b.id;
});

//执行,并重画列表 会调用iRsf.draw方法
iRsf.records();

iRsf 源码:

/**
 * 展现列表、排序、过滤(该三种操作以下简称为 RSF )
 * iRSF快速简单易用的实现列表,排序,过滤等功能
 * User: oshine
 * Date: 13-7-30
 * Time: 上午11:31
 */


function iRSFSource(setting)
{
    this.property = setting.property || "items";
    this.src = setting.src || {};

    this.clonePropertyList = function()
    {
        var tmp_data = [];
        for(var i in this.src[this.property])
        {
            tmp_data[i] = this.src[this.property][i];
        }
        return tmp_data;

    };

    this.clone = function()
    {
        var result = {};
        var tmp_data = this.clonePropertyList();
        return result[this.property] = tmp_data;
    }
}

function iRSFFilter()
{
    this.filters = {};

    this.filtering = function(src_data)
    {
        var ret = [],i= src_data.length-1;
        for(;i>=0;i--)
        {
            var flag = true;
            for(var j in this.filters)
            {
                var fn_filter = this.filters[j];
                if(typeof fn_filter == "function")
                {
                    flag = flag && fn_filter(src_data[i]);
                }

                if(!flag)
                {
                    break;
                }
            }

            if(flag)
            {
                ret.push(src_data[i]);
            }
        }

        return ret;
    };

    this.clearFilters = function()
    {
        for(var j in this.filters)
        {
            this.filters[j] = null;
            delete this.filters[j];
        }
    }
}

function iRSFSorter()
{
    this.sort = null;

    this.sorting = function(src_data)
    {
        if(this.sort === undefined || this.sort == null || typeof this.sort !== "function")
        {
            return src_data;
        }

        src_data.sort(this.sort);
        return src_data;
    }

}

function iRSF()
{
    this.iSource = new iRSFSource({src:{},property:"items"});
    this.sorter = new iRSFSorter();
    this.filter = new iRSFFilter();
    this.draw = null;

    this.setSource= function(setting)
    {
        this.iSource.src = setting.src || {};
        this.iSource.property = setting.property || "items";
    };

    this.records = function()
    {
        var $data = this.iSource.clonePropertyList();
        $data = this.filter.filtering($data);
        $data  = this.sorter.sorting($data);

        var result = {};
        result[this.iSource.property] = $data;

        if(this.draw !== undefined && this.draw !== null && typeof this.draw === "function")
        {
            this.draw(result);
        }
        return result;
    };

    this.addFilter = function(name,filter)
    {
        this.filter.filters[name] = filter;
    };

    this.removeFilter = function(name)
    {
        if(this.filter.filters[name] == undefined)
        {
            return true;
        }

        this.filter.filters[name] = null;
        delete this.filter.filters[name];
        return true;
    };

    this.setSort = function(sort)
    {
        this.sorter.sort = sort;
    };

    this.clearSort = function()
    {
        this.sorter.sort = null;
    }

}

  

  

作者: oShine.Q
出处:http://www.cnblogs.com/oshine/
作品oShine.Q 创作, 欢迎转载,但任何转载必须保留完整文章,在显要地方显示署名以及原文链接。如您有任何疑问,请给我留言。
原文地址:https://www.cnblogs.com/oshine/p/3884920.html