bootstrap自带的导出存在一个问题,它只能导出当前显示的数据。如果显示数据量太多的话,导出会死掉。现在扩展了一下他的导出。
修改tableExport.js文件
if (defaults.type == 'excel' || defaults.type == 'doc')
找到这一行代码,修改里面的内容,如下
$(el).find('thead').first().find(defaults.theadSelector).each(function () {
excel += "<tr>";
ForEachVisibleCell(this, 'th,td', rowIndex,
function (cell, row, col) {
if (cell != null) {
var name = parseString(cell, row, col);
if (name != '' && name != undefined)
{
excel += "<td style='";
for (var styles in defaults.excelstyles) {
if (defaults.excelstyles.hasOwnProperty(styles)) {
excel += defaults.excelstyles[styles] + ": " + $(cell).css(defaults.excelstyles[styles]) + ";";
}
}
excel += "'>" +name + "</td>";
}
}
});
//rowIndex++;
excel += '</tr>';
});
if (data != '' && data != undefined) {
data = JSON.parse(data);
if (data.hasOwnProperty('rows')) {
data = data.rows;
}
//遍历需要导出的数据
for (var i = 0; i < data.length; i++) {
excel += "<tr>";
var obj = data[i];
//遍历需要导出的字段
for (var j = 0; j < columns.length; j++) {
var col = columns[j];
//console.log(col.title+":"+col.visible);
//给td增加样式
//if ()
//{
//}
if (col.visible && obj.hasOwnProperty(col.field)) {
excel += "<td class='text-center' style='border-bottom:2px solid rgb(221, 221, 221), border-top:0px none rgb(51, 51, 51), border-left:0px none rgb(51, 51, 51), border-right:1px solid rgb(244, 244, 244)";
//for (var styles in defaults.excelstyles) {
// if (defaults.excelstyles.hasOwnProperty(styles)) {
// var css = '';
// if (styles == "0")
// css = '2px solid rgb(221, 221, 221)';
// if (styles == "1")
// css = '0px none rgb(51, 51, 51)';
// if (styles == "2")
// css = '0px none rgb(51, 51, 51)';
// if (styles == "3")
// css = '1px solid rgb(244, 244, 244)';
// excel += defaults.excelstyles[styles] + ": " + css + ";";
// }
//}
if (obj[col.field] == null) {
obj[col.field] = '';
}
excel += "'>" + obj[col.field] + "</td>";
// excel += "<td>" + obj[col.field] == null ? "" : obj[col.field] + "</td>";
// console.log(obj[col.field]);
}
//for (var item in obj) {
// console.log(item);//对象的属性
// console.log(obj[item]);//对象的值
//}
}
excel += "</tr>";
setNumbers(i);
}
}
//获取导出数据
function initServer(defaults) {
var tableOptions = $(el).bootstrapTable('getOptions');
var url = tableOptions.url;
var params = tableOptions.queryParams;
var columns = tableOptions.columns;
var result = '';
var data = $.extend(params(), { pagination: true })
$.post(url, data, function (res) {
init(defaults, res, columns)
});
}
function saveAs(fileName, data) {
//downloadFile(defaults.fileName + '.' + extension, 'data:application/vnd.ms-' + defaults.type + ';base64,' + base64data);
var blob = new Blob([data], { type: 'application/vnd.ms-' + defaults.type });
if (window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(blob, fileName);
}
else {
var encodedUri = encodeURI(data);
var DownloadLink = document.createElement("a");
DownloadLink.href = URL.createObjectURL(blob);
DownloadLink.download = fileName;
document.body.appendChild(DownloadLink);
if (document.createEvent) {
if (DownloadEvt == null)
DownloadEvt = document.createEvent('MouseEvents');
DownloadEvt.initEvent('click', true, false);
DownloadLink.dispatchEvent(DownloadEvt);
}
document.body.removeChild(DownloadLink);
}
showLoading(false);
//var DownloadLink = document.createElement('a');
//if (DownloadLink) {
// document.body.appendChild(DownloadLink);
// DownloadLink.style = 'display: none';
// DownloadLink.download = filename;
// DownloadLink.href = URL.createObjectURL(blob);
// if (document.createEvent) {
// if (DownloadEvt == null)
// DownloadEvt = document.createEvent('MouseEvents');
// DownloadEvt.initEvent('click', true, false);
// DownloadLink.dispatchEvent(DownloadEvt);
// }
// else if (document.createEventObject)
// DownloadLink.fireEvent('onclick');
// else if (typeof DownloadLink.onclick == 'function')
// DownloadLink.onclick();
// document.body.removeChild(DownloadLink);
//}
// link.href = URL.createObjectURL(blob);
}