如何在angularjs中的bootstrap表中导出xlsx文件,因为它允许下载xls文件

时间:2016-10-12 06:56:23

标签: angularjs

/**
 * @author zhixin wen <wenzhixin2010@gmail.com>
 * extensions: https://github.com/kayalshri/tableExport.jquery.plugin
 */

(function ($) {
    'use strict';
    var sprintf = $.fn.bootstrapTable.utils.sprintf;
 /**

这里是我们可以从bootstrap表下载的所有文件的typeName,我想通过xlsx TypeName下载xlsx文件      * /         var TYPE_NAME = {             json:'JSON',             xml:'XML',             png:'PNG',             csv:'CSV',             txt:'TXT',             sql:'SQL',             doc:'MS-Word',             excel:'Excel',             powerpoint:'MS-Powerpoint',             pdf:'PDF'         };

    $.extend($.fn.bootstrapTable.defaults, {
        showExport: true,
        exportDataType: 'all', // basic, all, selected
        // 'json', 'xml', 'png', 'csv', 'txt', 'sql', 'doc', 'excel', 'powerpoint', 'pdf'
        exportTypes: ['xml', 'csv', 'excel'],
        exportOptions: {}
    });

    $.extend($.fn.bootstrapTable.defaults.icons, {
        export: 'glyphicon-download-alt'
    });

    var BootstrapTable = $.fn.bootstrapTable.Constructor,
        _initToolbar = BootstrapTable.prototype.initToolbar;

    BootstrapTable.prototype.initToolbar = function () {
        this.showToolbar = this.options.showExport;

        _initToolbar.apply(this, Array.prototype.slice.apply(arguments));

        if (this.options.showExport) {
            var that = this,
                $btnGroup = this.$toolbar.find('>.btn-group'),
                $export = $btnGroup.find('div.export');

            if (!$export.length) {
                $export = $([
                    '<div class="export btn-group">',
                        '<button class="btn btn-default" title="Download" ' +
                            sprintf(' btn-%s', this.options.iconSize) +
                            ' dropdown-toggle" ' +
                            'data-toggle="dropdown" type="button">',
                            sprintf('<i class="%s %s"></i> ', this.options.iconsPrefix, this.options.icons.export),
                            '<span class="caret"></span>',
                        '</button>',
                        '<ul class="dropdown-menu" role="menu">',
                        '</ul>',
                    '</div>'].join('')).appendTo($btnGroup);

                var $menu = $export.find('.dropdown-menu'),
                    exportTypes = this.options.exportTypes;

                if (typeof this.options.exportTypes === 'string') {
                    var types = this.options.exportTypes.slice(1, -1).replace(/ /g, '').split(',');

                    exportTypes = [];
                    $.each(types, function (i, value) {
                        exportTypes.push(value.slice(1, -1));
                    });
                }
                $.each(exportTypes, function (i, type) {
                    if (TYPE_NAME.hasOwnProperty(type)) {
                        $menu.append(['<li data-type="' + type + '">',
                                '<a href="javascript:void(0)">',
                                    TYPE_NAME[type],
                                '</a>',
                            '</li>'].join(''));
                    }
                });

                $menu.find('li').click(function () {
                    var type = $(this).data('type'),
                        doExport = function () {
                            that.$el.tableExport($.extend({}, that.options.exportOptions, {
                                type: type,
                                escape: false,

                            }));
                        };

                    if (that.options.exportDataType === 'all' && that.options.pagination) {

                        //**----- Media Dataset table export hidden columns setting----**//
                        //** show Hidden columns **//
                        if (that.options.fileName != undefined && that.options.fileName == "MediaDatasetExport") {
                            that.columns.forEach(function (col) {
                                if (that.options.exportOptions.showColumn.indexOf(col.fieldIndex) != -1)
                                    col.visible = true;
                            });
                            that.initHeader();
                            that.initSearch();
                            that.initPagination();
                            that.initBody();
                        }
                        that.$el.one('load-success.bs.table page-change.bs.table', function () {
                            doExport();
                            that.togglePagination();
                        });
                        that.togglePagination();
                        //**----- Media Dataset table export hidden columns setting----**//
                        //** hide shown columns **//
                        if (that.options.fileName != undefined && that.options.fileName == "MediaDatasetExport") {
                            that.columns.forEach(function (col) {

                                if (that.options.exportOptions.showColumn.indexOf(col.fieldIndex) != -1)
                                    col.visible = false;

                            });
                            that.initHeader();
                            that.initSearch();
                            that.initPagination();
                            that.initBody();
                        }
                    } else if (that.options.exportDataType === 'selected') {
                        var data = that.getData(),
                            selectedData = that.getAllSelections();

                        that.load(selectedData);
                        doExport();
                        that.load(data);
                    } else {
                        doExport();
                    }
                });
            }
        }
    };
})(jQuery);

0 个答案:

没有答案