问题
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