html表到使用jquery的excel
除了需要添加一些东西之外,我有这个有效的jquery函数.我在桌子上有一个过滤器设置来隐藏列.当我隐藏列并单击导出"时,它将使用所有字段.我如何只导出未隐藏的表列?
I have this jquery function that works, except I need to add something. I have a filter setup on the table to hide columns. When I hide columns and click export it uses all fields regardless. How would I only export the table columns that are not hidden?
var write_to_excel = (function() {
var uri = 'data:application/vnd.ms-excel;base64,',
template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>',
base64 = function(s) {
return window.btoa(unescape(encodeURIComponent(s)))
},
format = function(s, c) {
return s.replace(/{(\w+)}/g, function(m, p) {
return c[p];
})
}
return function(table, name) {
if (!table.nodeType) table = document.getElementById(table) var ctx = {
worksheet: name || 'Worksheet',
table: table.innerHTML
}
window.location.href = uri + base64(format(template, ctx))
}
})()
我想我需要类似的东西.
I think I need something like.
if($(table).display.style!="none")
谢谢.
最简单的方法是在将表中的列传递给format函数之前,将其删除.由于您还是要舍弃该页面以导航到数据URI,因此保留隐藏的列并不重要.
The easiest way is to just remove the columns from the table before you pass it in to the format function. Since you're throwing away the page anyway to navigate to a data URI, it's not important to preserve the hidden columns.
删除隐藏列的方式取决于如何首先隐藏列.如果只是对隐藏的TD应用display:none,则可以通过对代码进行更改来实现.
The way you remove the hidden columns depends on how the columns are made hidden in the first place. If it's just by applying display:none to the hidden TDs, you can do it by making this change to the code.
return function(table, name) {
if (!table.nodeType)
table = document.getElementById(table);
$(table).find(':not(:visible)').remove();
var ctx = {
worksheet: name || 'Worksheet',
table: table.innerHTML
};
window.location.href = uri + base64(format(template, ctx));
};