layui 表格导出兼容IE,以及文件名自命名

1、兼容IE:

layui table.js修改源代码(地址:https://github.com/sentsin/layui ): lay/module/table.js

修改:  table.exportFile 方法

/**
	 * 修复IE不能下载的问题,支持IE10以上,此处是额外增加的代码,数据读取沿用table组件原有代码,为了尽量不破坏原来的代码,故此处会显得有些冗余。
	*/
	if (device.ie && navigator.msSaveBlob) {
		var dataTitle = [], dataMain = [];
		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) {
					vals.push(item2);
				});
			} else {
				table.eachCols(id, function (i3, item3) {
					if (item3.field && item3.type == 'normal' && !item3.hide) {
						i1 == 0 && dataTitle.push(item3.title || '');
						vals.push(item1[item3.field]);
					}
				});
			}
			dataMain.push(vals.join(','))
		});
		var filedata = "\ufeff" + dataTitle.join(',') + '\r\n' + dataMain.join('\r\n');
		if(fileName) {
			navigator.msSaveBlob(new Blob([decodeURIComponent(encodeURI(filedata))], {type: 'text/csv;charset=utf-8;'}),  (config.title || fileName + (config.index || '')) + '.' + type);
		}else {
			navigator.msSaveBlob(new Blob([decodeURIComponent(encodeURI(filedata))], {type: 'text/csv;charset=utf-8;'}),  (config.title || 'ajjkbb' + (config.index || '')) + '.' + type);
		}
		
		return true;
	}
	// 以下是原代码
    alink.href = 'data:'+ textType +';charset=utf-8,\ufeff'+ encodeURIComponent(function(){

2、表格导出自命名

① table.exportFile添加新参数: fileName

table.exportFile = function(id, data, type, fileName){}

②table.exportFile 内部导出js修改:

if(fileName) {
	navigator.msSaveBlob(new Blob([decodeURIComponent(encodeURI(filedata))], {type: 'text/csv;charset=utf-8;'}),  (config.title || fileName + (config.index || '')) + '.' + type);
}else {
	navigator.msSaveBlob(new Blob([decodeURIComponent(encodeURI(filedata))], {type: 'text/csv;charset=utf-8;'}),  (config.title || 'ajjkbb' + (config.index || '')) + '.' + type);
}

③前段导出方法调用

$(document).on('click','#export',function(){
    var data = [];
    table.exportFile(['标题','案件类型','立案日期'], data , "xls", "文件名");
}


     

 

 

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值