有什么方法可以在pivottable.js中设置pivot列的顺序吗?

时间:2019-05-24 11:50:20

标签: javascript pivottable.js

我想在我的网站中创建数据透视表。因此,我使用了pivottable.js创建数据透视表。

我可以使用以下JS创建数据透视表,但无法设置列的顺序。

以下代码用于生成数据透视表。

$(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

我们可以在列序列中首先获得 Emp2 而不是 Emp1 吗?

1 个答案:

答案 0 :(得分:1)

是的,您可以使用sorters配置选项。像

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

或者通常您可以传入任何旧的比较器:

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