简体   繁体   中英

Is there any way to set the sequence of the column of pivot in pivottable.js?

I want to create the pivot table in my website. So I've used the pivottable.js for creating the pivot table.

I'm able to create the pivot table using the following JS, but not able to set the sequence of the column.

following code is used to generate the pivot table.

$(document).ready(function(){
var sum = $.pivotUtilities.aggregatorTemplates.sum;
  var numberFormat = $.pivotUtilities.numberFormat;
  var intFormat = numberFormat({digitsAfterDecimal: 0});
    $("#raw_data_op").pivot(
        [
            ["id","mycount","type","date"],
            ["Name1","10","Emp1","1/1/2010"],
            ["Name1","13","Emp2","1/1/2010"],
            ["Name1","14","Emp1","1/2/2010"],
            ["Name1","10","Emp2","1/2/2010"],
            ["Name1","14","Emp1","1/3/2010"],
            ["Name1","12","Emp2","1/3/2010"],
            ["Name2","11","Emp1","1/1/2010"],
            ["Name2","13","Emp2","1/1/2010"],
            ["Name2","18","Emp1","1/2/2010"],
            ["Name2","10","Emp2","1/2/2010"],
            ["Name2","9","Emp1","1/3/2010"],
            ["Name2","17","Emp2","1/3/2010"],
            ["Name3","9","Emp1","1/1/2010"],
            ["Name3","0","Emp2","1/1/2010"],
            ["Name3","10","Emp1","1/2/2010"],
            ["Name3","12","Emp2","1/2/2010"],
            ["Name3","19","Emp1","1/3/2010"],
            ["Name3","10","Emp2","1/3/2010"],
        ], {
            rows: ["id"],
            cols: ["date","type"],
            aggregator: sum(intFormat)(["mycount"])
        });
});

Current Result:-

当前结果

Instead of Emp1 , can we get Emp2 first in the column sequence ?

Yes, you can use the sorters configuration option. Something like

sorters: {"type": $.pivotUtilities.sortAs(["Emp2", "Emp1"])}

Or in general you can just pass in any old comparator:

sorters: {"type": function(a,b){ return string_a.localeCompare(string_b) }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM