text_config.html 2.84 KB
<!DOCTYPE html>
<html>
    <head>
        <title>Pivot Demo</title>
        <!-- external libs from cdnjs -->
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>

        <!-- PivotTable.js libs from ../dist -->
        <link rel="stylesheet" type="text/css" href="../dist/pivot.css">
        <script type="text/javascript" src="../dist/pivot.js"></script>
        <style>
            body {font-family: Verdana;}
        </style>

        <!-- optional: mobile support with jqueryui-touch-punch -->
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui-touch-punch/0.2.3/jquery.ui.touch-punch.min.js"></script>

        <!-- for examples only! script to show code to user -->
        <script type="text/javascript" src="show_code.js"></script>
    </head>
    <body>
        <script type="text/javascript">
    // If you are loading a configuration which has been serialized to text,
    // you will need to merge it with an object containing the aggregators,
    // renderers, and sorters you wish to use, because functions cannot be
    // serialized to text.

    $(function(){
        $.getJSON("mps.json", function(mps) {

            // these are the functions you wish to use
            var functionsConfig = {
                aggregators: $.pivotUtilities.aggregators,
                renderers:   $.pivotUtilities.renderers,
                sorters: {
                    Province: $.pivotUtilities.sortAs(
                            ["British Columbia", "Alberta",
                            "Saskatchewan", "Manitoba",
                            "Territories", "Ontario", "Quebec",
                            "New Brunswick", "Prince Edward Island",
                            "Nova Scotia", "Newfoundland and Labrador"]),
                    Age: function(a,b){ return b-a; }
                }
            };

            // this is your saved/serialized config in a string
            var serializedConfig = '{"rows":["Province"], "cols":["Party"],'+
                            '"vals":["Age"],"aggregatorName":"Integer Sum"}';

            // deserialized it into an object
            var deserializedConfig = JSON.parse(serializedConfig)

            // merge the deserialized object with the functions object
            var mergedConfig = $.extend({}, functionsConfig, deserializedConfig);

            $("#output").pivotUI(mps, mergedConfig);
        });
     });
        </script>

        <p><a href="index.html">&laquo; back to PivotTable.js examples</a></p>
        <div id="output"></div>

    </body>
</html>