layui table 导出长数值后,自动变成科学计数法了

layui  table 导出长数值后,自动变成科学计数法了,网上找了一些方法,试了都不行,不过给了思路

完整代码如下,红色部分是我添加的

 //表格导出
    table.exportFile = function (id, data, type, name) {
        var that = this;

        data = data || table.clearCacheKey(table.cache[id]);
        type = type || 'csv';

        var config = thisTable.config[id] || {}
            , textType = ({
                csv: 'text/csv'
                , xls: 'application/vnd.ms-excel'
            })[type]
            , alink = document.createElement("a");

        if (device.ie) return hint.error('IE_NOT_SUPPORT_EXPORTS');

        alink.href = 'data:' + textType + ';charset=utf-8,\ufeff' + encodeURIComponent(function () {
            var dataTitle = [], dataMain = [], dataTotal = [];

            //表头和表体
            layui.each(data, function (i1, item1) {
                var vals = [];
                if (typeof id === 'object') { //如果 id 参数直接为表头数据
                    layui.each(id, function (i, item) {                       
                        i1 == 0 && dataTitle.push(item || '');
                    });
                    layui.each(table.clearCacheKey(item1), function (i2, item2) {
                        /^[0-9]{10,}$/.test(item2) ? (item2 += '\t') : "";
                        vals.push('"' + (item2 || '') + '"');
                    });
                } else {
                    table.eachCols(id, function (i3, item3) {
                        if (item3.field && item3.type == 'normal' && !item3.hide) {
                            var content = item1[item3.field];
                            if (content === undefined || content === null) content = '';

                            i1 == 0 && dataTitle.push(item3.title || '');
                            vals.push('"' + parseTempData(item3, content, item1, 'text') + '"');
                        }
                    });
                }
                dataMain.push(vals.join(','));
            });

            //表合计
            layui.each(that.dataTotal, function (key, value) {
                dataTotal.push(value);
            });

            return dataTitle.join(',') + '\r\n' + dataMain.join('\r\n') + '\r\n' + dataTotal.join(',');
        }());

        alink.download = (name||config.title || 'table_' + (config.index || '')) + '.' + type;
        document.body.appendChild(alink);
        alink.click();
        document.body.removeChild(alink);
    };

 

posted @ 2023-02-06 21:38  sharestone  阅读(300)  评论(0编辑  收藏  举报