地质所 沉降监测网建设项目
chenhuan
2024-05-16 0fdd42e318f51f9e3c6581473416af1cca69877f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
 * @author zhixin wen <wenzhixin2010@gmail.com>
 * extensions: https://github.com/kayalshri/tableExport.jquery.plugin
 */
 
(function ($) {
    'use strict';
    var sprintf = $.fn.bootstrapTable.utils.sprintf;
 
    var TYPE_NAME = {
        csv: 'CSV',
        txt: 'TXT',
        doc: 'Word',
        excel: 'Excel'
    };
 
    $.extend($.fn.bootstrapTable.defaults, {
        showExport: false,
        exportDataType: 'all', // basic, all, selected
        exportTypes: ['csv', 'txt', 'doc', 'excel'],
        exportOptions: {
            ignoreColumn: [0]  //忽略列索引
        }
    });
 
    $.extend($.fn.bootstrapTable.defaults.icons, {
        export: 'glyphicon glyphicon-save'
    });
 
    $.extend($.fn.bootstrapTable.locales, {
        formatExport: function () {
            return '导出';
        }
    });
    $.extend($.fn.bootstrapTable.defaults, $.fn.bootstrapTable.locales);
 
    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' +
                            sprintf(' btn-%s', this.options.buttonsClass) +
                            sprintf(' btn-%s', this.options.iconSize) +
                            ' dropdown-toggle" ' +
                            'title="' + this.options.formatExport() + '" ' +
                            '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) {
                        that.$el.one(that.options.sidePagination === 'server' ? 'post-body.bs.table' : 'page-change.bs.table', function () {
                            doExport();
                            that.togglePagination();
                        });
                        that.togglePagination();
                    } else if (that.options.exportDataType === 'selected') {
                        //修改sidePagination属性为server无法导出选中数据
                        var trs = that.$body.children(); 
                        for (var i = 0; i < trs.length; i++) {
                            var $this = $(trs[i]);
                            if(!$this.find(sprintf('[name="%s"]',that.options.selectItemName)).prop('checked')){
                              $this['hide']();
                         }}
                        doExport();
                        that.getRowsHidden(true);
                    } else {
                        doExport();
                    }
                });
            }
        }
    };
})(jQuery);