Subversion-Projekte lars-tiefland.ci

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
776 lars 1
/**
2
 * @author zhixin wen <wenzhixin2010@gmail.com>
3
 * extensions: https://github.com/kayalshri/tableExport.jquery.plugin
4
 */
5
 
6
(function ($) {
7
    'use strict';
8
 
9
    var TYPE_NAME = {
10
        json: 'JSON',
11
        xml: 'XML',
12
        png: 'PNG',
13
        csv: 'CSV',
14
        txt: 'TXT',
15
        sql: 'SQL',
16
        doc: 'MS-Word',
17
        excel: 'MS-Excel',
18
        powerpoint: 'MS-Powerpoint',
19
        pdf: 'PDF'
20
    };
21
 
22
    $.extend($.fn.bootstrapTable.defaults, {
23
        showExport: false,
24
        exportDataType: 'basic', // basic, all, selected
25
        // 'json', 'xml', 'png', 'csv', 'txt', 'sql', 'doc', 'excel', 'powerpoint', 'pdf'
26
        exportTypes: ['json', 'xml', 'csv', 'txt', 'sql', 'excel'],
27
        exportOptions: {}
28
    });
29
 
30
    var BootstrapTable = $.fn.bootstrapTable.Constructor,
31
        _initToolbar = BootstrapTable.prototype.initToolbar;
32
 
33
    BootstrapTable.prototype.initToolbar = function () {
34
        this.showToolbar = this.options.showExport;
35
 
36
        _initToolbar.apply(this, Array.prototype.slice.apply(arguments));
37
 
38
        if (this.options.showExport) {
39
            var that = this,
40
                $btnGroup = this.$toolbar.find('>.btn-group'),
41
                $export = $btnGroup.find('div.export');
42
 
43
            if (!$export.length) {
44
                $export = $([
45
                    '<div class="export btn-group">',
46
                        '<button class="btn btn-default dropdown-toggle" ' +
47
                            'data-toggle="dropdown" type="button">',
48
                            '<i class="glyphicon glyphicon-export icon-share"></i> ',
49
                            '<span class="caret"></span>',
50
                        '</button>',
51
                        '<ul class="dropdown-menu" role="menu">',
52
                        '</ul>',
53
                    '</div>'].join('')).appendTo($btnGroup);
54
 
55
                var $menu = $export.find('.dropdown-menu'),
56
                    exportTypes = this.options.exportTypes;
57
 
58
                if (typeof this.options.exportTypes === 'string') {
59
                    var types = this.options.exportTypes.slice(1, -1).replace(/ /g, '').split(',');
60
 
61
                    exportTypes = [];
62
                    $.each(types, function (i, value) {
63
                        exportTypes.push(value.slice(1, -1));
64
                    });
65
                }
66
                $.each(exportTypes, function (i, type) {
67
                    if (TYPE_NAME.hasOwnProperty(type)) {
68
                        $menu.append(['<li data-type="' + type + '">',
69
                                '<a href="javascript:void(0)">',
70
                                    TYPE_NAME[type],
71
                                '</a>',
72
                            '</li>'].join(''));
73
                    }
74
                });
75
 
76
                $menu.find('li').click(function () {
77
                    var type = $(this).data('type'),
78
                        doExport = function () {
79
                            that.$el.tableExport($.extend({}, that.options.exportOptions, {
80
                                type: type,
81
                                escape: false
82
                            }));
83
                        };
84
 
85
                    if (that.options.exportDataType === 'all' && that.options.pagination) {
86
                        that.$el.one('load-success.bs.table page-change.bs.table', function () {
87
                            doExport();
88
                            that.togglePagination();
89
                        });
90
                        that.togglePagination();
91
                    } else if (that.options.exportDataType === 'selected') {
92
                        var data = that.getData(),
93
                            selectedData = that.getAllSelections();
94
 
95
                        that.load(selectedData);
96
                        doExport();
97
                        that.load(data);
98
                    } else {
99
                        doExport();
100
                    }
101
                });
102
            }
103
        }
104
    };
105
})(jQuery);