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

Call renderer from outside #639

Open EmilMoe opened 7 years ago

EmilMoe commented 7 years ago

I have modified the TSV Export a bit so it will stream a CSV file instead. However now I would like to add a button that says Export to CSV instead of having to select the dropdown.

How can I read the current renderer and set a renderer from this button?

nicolaskruchten commented 7 years ago

Check out this example for how to listen to configuration changes and force configuration changes: http://nicolas.kruchten.com/pivottable/examples/save_restore.html

EmilMoe commented 7 years ago

Thanks. That was a big help. Can I hide the TSV Export from the menu?

nithishanf commented 7 years ago

@EmilMoe Hi, How did you modify TSV Export to stream a CSV file instead. Please help.

EmilMoe commented 7 years ago

@nithishanf yes I changed so it streams to a TSB file, removes the tsv entry in the select and switches back after streaming to the previous render. I can give you my code tomorrow

nithishanf commented 7 years ago

@EmilMoe Thanks. I am in need of a Export to Excel. This CSV export can help me to an extent. Please share with me the code.

EmilMoe commented 7 years ago

I modified the export_renderer.js to

(function() {
    var callWithJQuery;

    callWithJQuery = function(pivotModule) {
        if (typeof exports === "object" && typeof module === "object") {
            return pivotModule(require("jquery"));
        } else if (typeof define === "function" && define.amd) {
            return define(["jquery"], pivotModule);
        } else {
            return pivotModule(jQuery);
        }
    };

    callWithJQuery(function($) {
        return $.pivotUtilities.export_renderers = {
            "TSV Export": function(pivotData, opts) {
                var agg, colAttrs, colKey, colKeys, defaults, i, j, k, l, len, len1, len2, len3, len4, len5, m, n, r, result, row, rowAttr, rowAttrs, rowKey, rowKeys, text;
                defaults = {
                    localeStrings: {}
                };
                opts = $.extend(true, {}, defaults, opts);
                rowKeys = pivotData.getRowKeys();
                if (rowKeys.length === 0) {
                    rowKeys.push([]);
                }
                colKeys = pivotData.getColKeys();
                if (colKeys.length === 0) {
                    colKeys.push([]);
                }
                rowAttrs = pivotData.rowAttrs;
                colAttrs = pivotData.colAttrs;
                result = [];
                row = [];
                for (i = 0, len = rowAttrs.length; i < len; i++) {
                    rowAttr = rowAttrs[i];
                    row.push(rowAttr);
                }
                if (colKeys.length === 1 && colKeys[0].length === 0) {
                    row.push(pivotData.aggregatorName);
                } else {
                    for (j = 0, len1 = colKeys.length; j < len1; j++) {
                        colKey = colKeys[j];
                        row.push(colKey.join("-"));
                    }
                }
                result.push(row);
                for (k = 0, len2 = rowKeys.length; k < len2; k++) {
                    rowKey = rowKeys[k];
                    row = [];
                    for (l = 0, len3 = rowKey.length; l < len3; l++) {
                        r = rowKey[l];
                        row.push(r);
                    }
                    for (m = 0, len4 = colKeys.length; m < len4; m++) {
                        colKey = colKeys[m];
                        agg = pivotData.getAggregator(rowKey, colKey);
                        if (agg.value() != null) {
                            row.push(agg.value());
                        } else {
                            row.push("");
                        }
                    }
                    result.push(row);
                }
                text = "";
                for (n = 0, len5 = result.length; n < len5; n++) {
                    r = result[n];
                    text += r.join("\t") + "\n";
                }

                function download(filename, text) {
                    var pom = document.createElement('a');
                    pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
                    pom.setAttribute('download', filename);

                    if (document.createEvent) {
                        var event = document.createEvent('MouseEvents');
                        event.initEvent('click', true, true);
                        pom.dispatchEvent(event);
                    }
                    else {
                        pom.click();
                    }
                }

                download('download.tab', text);
            }
        };
    });

}).call(this);

//# sourceMappingURL=export_renderers.js.map

In the pivot tool I use this function

        function exportCSV() {
            var oldConfig    = $("#pivot").data("pivotUIOptions");
            var rendererName = $("#pivot").data("pivotUIOptions").rendererName;
            var newConfig    = oldConfig;

            newConfig.rendererName = 'TSV Export';
            $("#pivot").pivotUI(pivotData, newConfig, true);

            setTimeout(function() {
                oldConfig.rendererName = rendererName;
                $("#pivot").pivotUI(pivotData, oldConfig, true);

                $('#pivot .pvtRenderer option').each(function(k ,v) {
                    if ($(v).val() == 'TSV Export')
                        $(v).remove();
                });
            }, 1);

And this

                $('#pivot .pvtRenderer option').each(function(k ,v) {
                    if ($(v).val() == 'TSV Export')
                        $(v).remove();
                });
<button onclick="exportCSV()" class="btn btn-primary">Export to TAB (Excel)</button>
abhikvnair commented 6 years ago

@EmilMoe Sir, Please share the working code for export to excel

abhikvnair commented 6 years ago

function exportCSV() { var oldConfig = $("#pivot").data("pivotUIOptions"); var rendererName = $("#pivot").data("pivotUIOptions").rendererName; var newConfig = oldConfig;

        newConfig.rendererName = 'TSV Export';
        $("#pivot").pivotUI(pivotData, newConfig, true);

        setTimeout(function() {
            oldConfig.rendererName = rendererName;
            $("#pivot").pivotUI(pivotData, oldConfig, true);

            $('#pivot .pvtRenderer option').each(function(k ,v) {
                if ($(v).val() == 'TSV Export')
                    $(v).remove();
            });
        }, 1);

And this

            $('#pivot .pvtRenderer option').each(function(k ,v) {
                if ($(v).val() == 'TSV Export')
                    $(v).remove();
            });

In which file above scripts to be added?