我以这种方式拥有my project
:https://jsfiddle.net/Eufragio/u342qgoz/1/
导出excel
时,我需要一个订单或一种更直观的方式来显示我的结果
$(document).ready( function () {
var table = $('#example').DataTable({
dom: 'Btirp',
buttons: [{
extend: 'csvHtml5',
text: 'CSV',
filename: 'csv_file',
footer: true
},
{
extend: 'excelHtml5',
text: 'Excel',
filename: 'excel_file',
footer: true
}],
//Total General
"footerCallback": function (row, data, start, end, display) {
var api = this.api(),
data;
// Remove the formatting to get integer data for summation
var intVal = function (i) {
return typeof i === 'string' ?
i.replace(/[\L,]/g, '') * 1 :
typeof i === 'number' ?
i : 0;
};
/*
// Total over all pages // Total en todas las páginas
total = api
.column(5)
.data()
.reduce(function(a, b) {
return intVal(a) + intVal(b);
}, 0);
// Total over this page // Total sobre esta pagina
pageTotal = api
.column(5, {
page: 'current'
})
.data()
.reduce(function(a, b) {
return intVal(a) + intVal(b);
}, 0);
// Update footer //Pie de página de actualización
$(api.column([5, 3]).footer()).html(
// '' + pageTotal + ' ( L' + total + ' total)'
//'' + total.toFixed(2)
'' + total
);
*/
// Total over all pages // Total en todas las páginas
total = api
.column(3)
.data()
.reduce(function (a, b) {
return intVal(a) + intVal(b);
}, 0);
// Total over this page // Total sobre esta pagina
pageTotal = api
.column(3, {
page: 'current'
})
.data()
.reduce(function (a, b) {
return intVal(a) + intVal(b);
}, 0);
// Update footer //Pie de página de actualización
$(api.column(3).footer()).html(
// '' + pageTotal + ' ( L' + total + ' total)'
//'' + total.toFixed(2)
'' + total
);
},
"columnDefs": [{
"visible": false,
"targets": 2
}],
"order": [
[2, 'asc']
],
"displayLength": 25,
"drawCallback": function (settings) {
var api = this.api();
var rows = api.rows({
page: 'all'
}).nodes();
var last = null;
// Remove the formatting to get integer data for summation
var intVal = function (i) {
return typeof i === 'string' ?
i.replace(/[\$,]/g, '') * 1 :
typeof i === 'number' ?
i : 0;
};
var groupTotal = {};
api.column(2, {
page: 'all'
}).data().each(function (group, i) {
group_assoc = group.replace(' ', "_");
console.log(group_assoc);
if (typeof groupTotal[group_assoc] != 'undefined') {
groupTotal[group_assoc] = groupTotal[group_assoc] + intVal(api.column(5).data()[i]);
/*
$(api.column(2).footer()).html(
'' + total[group_assoc]
);
*/
} else {
groupTotal[group_assoc] = intVal(api.column(5).data()[i]);
}
if (last !== group) {
$(rows).eq(i).before(
'<tr class="group"><td colspan="4">' + group + '</td><td class="' + group_assoc + '"></td></tr>'
);
last = group;
}
});
var footerText = [];
var footerTotal = [];
for (var key in groupTotal) {
$("." + key).html("L" + groupTotal[key].toFixed(2));
footerText.push(key); // Get array of group names
footerTotal.push("L" + groupTotal[key].toFixed(2)); // Get array of group totals
}
// Update footer with group names, each on a newline
$(api.column(4).footer()).html(
footerText.join('<br>')
);
// Update footer with group totals, each on a newline
$(api.column(5).footer()).html(
footerTotal.join('<br>')
);
}
});
});
我的problem
是exporting
到excel
时,结果以这种方式带给我:
您可以通过哪种方式显示此result
或推荐的其他方式
答案 0 :(得分:0)
旧话题,但也许有人会寻找解决方案。 我在不支持“ \ r \ n”字符的DataTables 1.10.10版本上遇到了相同的问题。 经过数小时的搜索,我找到了解决方案,足以将DataTables版本升级到最新版本(当前为1.10.20)。
现在,使用此代码,一切都可以正常工作:
extend: 'excelHtml5',
filename: 'file_name',
text: 'Save as Excel',
exportOptions: {
format: {
body: function(data, column, row) {
if (typeof data === 'string' || data instanceof String) {
data = data.replace(/<br\s*\/?>/ig, "\r\n");
}
return data;
}
}
}