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

♀尐吖头ヾ 提交于 2020-01-16 17:03:11

问题


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 ?


回答1:


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) }


来源:https://stackoverflow.com/questions/56292094/is-there-any-way-to-set-the-sequence-of-the-column-of-pivot-in-pivottable-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!