nicolaskruchten / pivottable

Open-source Javascript Pivot Table (aka Pivot Grid, Pivot Chart, Cross-Tab) implementation with drag'n'drop.
https://pivottable.js.org/
MIT License
4.36k stars 1.08k forks source link

Error on saving configuration #1096

Open iwalucas opened 5 years ago

iwalucas commented 5 years ago

I have a pivottable that when I try to save, throws me some errors.... If I try to JSON.stringfy the pivotUIOptions, this is what I get (console output):

var x = $("#output").data("pivotUIOptions"); x {rendererOptions: {…}, localeStrings: {…}, derivedAttributes: {…}, aggregators: {…}, renderers: {…}, …}

JSON.stringify(x) "{"rendererOptions":{"localeStrings":{"renderError":"An error occurred rendering the PivotTable results.","computeError":"An error occurred computing the PivotTable results.","uiRenderError":"An error occurred rendering the PivotTable UI.","selectAll":"Select All","selectNone":"Select None","tooMany":"(too many to list)","filterResults":"Filter values","apply":"Apply","cancel":"Cancel","totals":"Totals","vs":"vs","by":"by"},"table":{}},"localeStrings":{"renderError":"An error occurred rendering the PivotTable results.","computeError":"An error occurred computing the PivotTable results.","uiRenderError":"An error occurred rendering the PivotTable UI.","selectAll":"Select All","selectNone":"Select None","tooMany":"(too many to list)","filterResults":"Filter values","apply":"Apply","cancel":"Cancel","totals":"Totals","vs":"vs","by":"by"},"derivedAttributes":{},"aggregators":{},"renderers":{},"hiddenAttributes":[],"hiddenFromAggregators":[],"hiddenFromDragDrop":[],"menuLimit":500,"cols":[],"rows":[],"vals":[],"rowOrder":"key_a_to_z","colOrder":"key_a_to_z","exclusions":{},"inclusions":{},"unusedAttrsVertical":85,"autoSortUnusedAttrs":false,"onRefresh":null,"showUI":true,"sorters":{},"inclusionsInfo":{},"aggregatorName":"Count","rendererName":"Table"}"

I tried to follow the "https://pivottable.js.org/examples/save_restore.html" tutorial, but I need to save it to DB and not cookie, so I need a full JSON string.

If I try:

y = JSON.parse(JSON.stringify(x));

I get a partial json, like:

aggregators: {}

instead of the original:

aggregators: {Count: ƒ, Count Unique Values: ƒ, List Unique Values: ƒ, Sum: ƒ, Integer Sum: ƒ, …}

mpf82 commented 5 years ago

Functions are not JSON serializable, that's the reason why aggregators and renderers are deleted on the deep-copied config object in the save/restore example.

But the example also demonstrates that it's not neccessary to save and restore the functions, they should already be a part of your pivot table, you should only want to restore the selection.

Note: You can merge/extend an existing (options) object.

iwalucas commented 5 years ago

But if I try to apply the saved config I got an error b/c the aggregator is empty