From 8e93d0193742044ebf8208d49b34ac9fa52fc550 Mon Sep 17 00:00:00 2001 From: Nicolas Kruchten Date: Mon, 23 Feb 2015 20:29:57 -0500 Subject: [PATCH 1/6] checkpoint, broken --- examples/mps_prepop.html | 5 +++++ pivot.coffee | 32 +++++++++++++++++++++++--------- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/examples/mps_prepop.html b/examples/mps_prepop.html index 2a690ced..dbed3f08 100644 --- a/examples/mps_prepop.html +++ b/examples/mps_prepop.html @@ -23,6 +23,11 @@ return mp["Gender"] == "Male" ? 1 : -1; } }, + sortProvider: function(attr){ + if(attr == "Age"){ + return function(a,b){return b-a;}; + } + }, rows: ["Province"], cols: ["Party"], aggregatorName: "Integer Sum", diff --git a/pivot.coffee b/pivot.coffee index f3708f8c..998f8a0d 100644 --- a/pivot.coffee +++ b/pivot.coffee @@ -200,6 +200,13 @@ callWithJQuery ($) -> return (if a1 > b1 then 1 else -1) a.length - b.length + getSort = (sortProvider, attr) -> + sort = sortProvider(attr) + if $.isFunction(sort) + return sort + else + return naturalSort + #expose these to the outside world $.pivotUtilities = {aggregatorTemplates, aggregators, renderers, derivers, locales, naturalSort, numberFormat} @@ -215,6 +222,7 @@ callWithJQuery ($) -> @colAttrs = opts.cols @rowAttrs = opts.rows @valAttrs = opts.vals + @sortProvider = opts.sortProvider @tree = {} @rowKeys = [] @colKeys = [] @@ -263,15 +271,19 @@ callWithJQuery ($) -> PivotData.forEachRecord input, {}, (record) -> result.push record return result - natSort: (as, bs) => naturalSort(as, bs) - - arrSort: (a,b) => @natSort a.join(), b.join() + arrSort: (attrs) => + sorters = getSort(@sortProvider, attr) for attr in attrs + (a,b) => + for i, sorter of sorters + comparison = sorter(a[i], b[i]) + return comparison if comparison != 0 + return 0 sortKeys: () => if not @sorted - @rowKeys.sort @arrSort - @colKeys.sort @arrSort - @sorted = true + @sorted = true + @rowKeys.sort @arrSort(@rowAttrs) + @colKeys.sort @arrSort(@colAttrs) getColKeys: () => @sortKeys() @@ -481,6 +493,7 @@ callWithJQuery ($) -> filter: -> true aggregator: aggregatorTemplates.count()() aggregatorName: "Count" + sortProvider: -> derivedAttributes: {}, renderer: pivotTableRenderer rendererOptions: null @@ -523,6 +536,7 @@ callWithJQuery ($) -> rendererOptions: localeStrings: locales[locale].localeStrings onRefresh: null filter: -> true + sortProvider: -> localeStrings: locales[locale].localeStrings existingOpts = @data "pivotUIOptions" @@ -602,7 +616,7 @@ callWithJQuery ($) -> checkContainer = $("
").addClass("pvtCheckContainer").appendTo(valueList) - for k in keys.sort(naturalSort) + for k in keys.sort(getSort(opts.sortProvider, c)) v = axisValues[c][k] filterItem = $("