iRSF 빠 르 고 간단 하 며 사용 하기 쉬 운 구현 목록, 정렬, 필터 기능

4058 단어 정렬
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;

    }



}


  

좋은 웹페이지 즐겨찾기