wzp
2021-07-19 e65183d31755a0e5fae4bf428435d2e0fd6afdc5
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
/**
 * jQuery.bsgrid v1.38 by @Baishui2004
 * Copyright 2014 Apache v2 License
 * https://github.com/baishui2004/jquery.bsgrid
 */
/**
 * require common.js, util.js.
 *
 * @author Baishui2004
 * @Date July 1, 2014
 */
(function ($) {
 
    $.bsgrid_export = {
 
        // defaults settings
        defaults: {
            url: '', // export url
            exportFileName: 'export', // export file name, not contains file suffix
            colsProperties: {
                width: 100,
                align: 'center',
                exportAttr: 'w_export',
                indexAttr: 'w_index',
                widthAttr: 'width',
                alignAttr: 'w_align'
            },
            colWidthPercentmultiplier: 14, // if set column width N%, then column width will reset N*14
            // request params name
            requestParamsName: {
                exportFileName: 'exportFileName',
                colNames: 'dataNames',
                colIndexs: 'dataIndexs',
                colWidths: 'dataLengths',
                colAligns: 'dataAligns'
            }
        },
 
        /**
         * do export.
         *
         * @param exportCols
         * @param exportParams A Search String, A Object or A jquery serialize Array
         * @param settings
         */
        doExport: function (exportCols, exportParams, settings) {
            if (exportParams == undefined) {
                exportParams = {};
            }
 
            var exportSettings = {};
            if (settings == undefined) {
                settings = {};
            }
            $.extend(true, exportSettings, $.bsgrid_export.defaults, settings);
 
            var colNames = '', colIndexs = '', colWidths = '', colAligns = '';
            for (var i = 0; i < exportCols.length; i++) {
                if ($.trim(exportCols.eq(i).attr(exportSettings.colsProperties.exportAttr)) != 'false') {
                    // column name, get form column's text(), use jquery
                    colNames = colNames + ',' + $.trim(exportCols.eq(i).text());
                    colIndexs = colIndexs + ',' + $.trim(exportCols.eq(i).attr(exportSettings.colsProperties.indexAttr));
 
                    var colWidthStr = $.trim(exportCols.eq(i).attr(exportSettings.colsProperties.widthAttr)).toLocaleLowerCase();
                    var colWidth = exportSettings.colsProperties.width;
                    if (isNaN(colWidthStr)) {
                        if (colWidthStr.endWith('px')) {
                            colWidth = parseInt(colWidthStr.replace('px', ''));
                        } else if (colWidthStr.endWith('%')) {
                            colWidthStr = colWidthStr.replace('%', '');
                            if (!isNaN(colWidthStr)) {
                                colWidth = exportSettings.colWidthPercentmultiplier * parseInt(colWidthStr);
                            }
                        }
                    }
                    colWidths = colWidths + ',' + colWidth;
 
                    var colAlign = $.trim(exportCols.eq(i).attr(exportSettings.colsProperties.alignAttr));
                    if (colAlign == '') {
                        colAlign = exportSettings.colsProperties.align;
                    }
                    colAligns = colAligns + ',' + colAlign;
                }
            }
 
            var exportParamsStr;
            if ((typeof exportParams).toLowerCase() == 'string' || exportParams instanceof String) {
                exportParamsStr = (exportParams.startWith('&') ? exportParams.substring(1) : exportParams);
            } else {
                exportParamsStr = $.bsgrid.param(exportParams, true);
            }
            document.location.href = exportSettings.url + (exportSettings.url.indexOf('?') < 0 ? '?' : '&')
                + exportSettings.requestParamsName.exportFileName + '=' + encodeURIComponent(encodeURIComponent(exportSettings.exportFileName))
                + '&' + exportSettings.requestParamsName.colNames + '=' + encodeURIComponent(encodeURIComponent(colNames.substring(1)))
                + '&' + exportSettings.requestParamsName.colIndexs + '=' + colIndexs.substring(1)
                + '&' + exportSettings.requestParamsName.colWidths + '=' + colWidths.substring(1)
                + '&' + exportSettings.requestParamsName.colAligns + '=' + colAligns.substring(1)
                + (exportParamsStr.length == 0 ? '' : ('&' + exportParamsStr));
        }
 
    };
 
})(jQuery);