SpreadJS的排序

发布时间:2015/08/28 00:08 发布者:frank.zhang

返回博客中心

SpreadJS 是一款基于 HTML5 和 jQuery 技术的插件,支持多种方式的排序。本文主要介绍SpreadJS排序的四种方式。

1.升序或降序排序

    $(document).ready(function() {
        var spread = new GcSpread.Sheets.Spread($("#ss").get(0), {
            sheetCount: 3
        });
        var activeSheet = spread.getActiveSheet();
        activeSheet.setValue(0, 0, 10);
        activeSheet.setValue(1, 0, 100);
        activeSheet.setValue(2, 0, 50);
        activeSheet.setValue(3, 0, 40);
        activeSheet.setValue(4, 0, 80);
        activeSheet.setValue(5, 0, 1);
        activeSheet.setValue(6, 0, 65);
        activeSheet.setValue(7, 0, 20);
        activeSheet.setValue(8, 0, 30);
        activeSheet.setValue(9, 0, 35);
        $("#button1").click(function() {
            //Sort Column 1 in ascending order on button click.
            $("#ss").data("spread").getActiveSheet().sortRange(-1, 0, -1, 1, true, [{
                index: 0,
                ascending: true
            }]);
        });
        $("#button2").click(function() {
            //Sort Column 1 in descending order on button click.
            $("#ss").data("spread").getActiveSheet().sortRange(-1, 0, -1, 1, true, [{
                index: 0,
                ascending: false
            }]);
        });
    });

 

排序前:

2015-08-28_113904

 

排序后:

2015-08-28_114333

 

2.使用多个关键字进行排序

        $(document).ready(function () {
            var spread = new GcSpread.Sheets.Spread($("#ss").get(0),{sheetCount:3});
            var activeSheet = spread.getActiveSheet();
						activeSheet.setRowCount(6);
            activeSheet.setValue(0, 0, 10);
            activeSheet.setValue(1, 0, 100);
            activeSheet.setValue(2, 0, 100);
            activeSheet.setValue(3, 0, 10);
            activeSheet.setValue(4, 0, 5);
            activeSheet.setValue(5, 0, 10);
            activeSheet.setValue(0, 1, 10);
            activeSheet.setValue(1, 1, 40);
            activeSheet.setValue(2, 1, 10);
            activeSheet.setValue(3, 1, 20);
            activeSheet.setValue(4, 1, 10);
            activeSheet.setValue(5, 1, 40);
            
            $("#button1").click(function(){
                //Create a SortInfo object where 1st Key:Column1/2nd Key:Column2.
                var sortInfo = [
                {index:0, ascending:true},
                {index:1, ascending:true}];
                ///Execute sorting which targets all rows based on the created sorting conditions.
                $("#ss").data("spread").getActiveSheet().sortRange(0, -1, 6, -1, true, sortInfo);
            });
            
            $("#button2").click(function(){
                //Create a SortInfo object where 1st Key:Column1/2nd Key:Column2.
                var sortInfo = [
                {index:0, ascending:false},
                {index:1, ascending:false}];
                ///Execute sorting which targets all rows based on the created sorting conditions.
                $("#ss").data("spread").getActiveSheet().sortRange(0, -1, 6, -1, true, sortInfo);
            });
        });

 

排序前:

2015-08-28_114503

排序后:

2015-08-28_114519

 

3.使用Filter进行排序

        $(document).ready(function () {
            var spread = new GcSpread.Sheets.Spread($("#ss").get(0),{sheetCount:3});
            var activeSheet = spread.getActiveSheet();
						activeSheet.setValue(0, 0, 10);
            activeSheet.setValue(1, 0, 100);
            activeSheet.setValue(2, 0, 50);
            activeSheet.setValue(3, 0, 40);
            activeSheet.setValue(4, 0, 80);
            activeSheet.setValue(5, 0, 1);
            activeSheet.setValue(6, 0, 65);
            activeSheet.setValue(7, 0, 20);
            activeSheet.setValue(8, 0, 30);
            activeSheet.setValue(9, 0, 35);
            var cellrange =new GcSpread.Sheets.Range(0, 0, 10, 1);
            var hideRowFilter =new GcSpread.Sheets.HideRowFilter(cellrange);
            activeSheet.rowFilter(hideRowFilter);
				});

 

运行效果:

2015-08-28_114953

 

4.使用自定义Filter进行排序

    		function CustomFilter(){};
				CustomFilter.prototype = {
         	conditionType: "CustomFilter",
         	type:"CustomFilter",
         	evaluate: function (evaluator, row, col) {
         	    var value = evaluator.getValue(row, col);
         	
         	    if(value !== null && value >= 10 && value <= 50){
         	        //Return True only when the following conditions are satisfied.
         	        // (1)Values are entered.
         	        // (2)Values are not lower than 10.
         	        // (3)Values are not greater than 50.
         	        return true;
         	    }else{
         	        return false;
         	    }
         	},
         	toJSON:function(){
         		var jsonData = {};
         		jsonData.conditionType = this.conditionType;
         		jsonData.type = "CustomFilter";
         		return jsonData;
         	},
         		fromJSON:function(data){
         			
         		}
				};
        $(document).ready(function () {
            spread = new GcSpread.Sheets.Spread($("#ss").get(0),{sheetCount:3});
            var activeSheet = spread.getActiveSheet();
            
            activeSheet.setValue(0, 0, 10);
            activeSheet.setValue(1, 0, 100);
            activeSheet.setValue(2, 0, 50);
            activeSheet.setValue(3, 0, 40);
            activeSheet.setValue(4, 0, 80);
            activeSheet.setValue(5, 0, 1);
            activeSheet.setValue(6, 0, 65);
            activeSheet.setValue(7, 0, 20);
            activeSheet.setValue(8, 0, 30);
            activeSheet.setValue(9, 0, 35);
            
            //Set a row Filter.
            var rowFilter = new GcSpread.Sheets.HideRowFilter(new GcSpread.Sheets.Range(0, 0, 7, 1));
            activeSheet.rowFilter(rowFilter);
            rowFilter.addFilterItem(0, new CustomFilter());
            rowFilter.filter(0);
				});

 

运行效果:

2015-08-28_115053

 

示例下载:

这就是你想要的SpreadJS,快来官网了解并下载它吧!


关于葡萄城

赋能开发者!葡萄城是专业的集开发工具、商业智能解决方案、低代码开发平台于一身的软件和服务提供商,为超过 75% 的全球财富 500 强企业提供服务。葡萄城专注控件软件领域30年,希望通过模块化的开发控件、灵活的低代码应用开发平台等一系列开发工具、解决方案和服务,帮助开发者快速响应复杂多变的业务需求,最大程度地发挥开发者的才智和潜能,让开发者的 IT 人生更从容更美好。

了解详情,请访问葡萄城官网