当页面上有多个表时,我无法使用 table2CSV.js 导出。
第一个将正常工作,但任何其他按钮将导出一个空白文件。有任何想法吗?如果您需要更多信息,请告知。
我想补充一点,它始终是页面上第一个正常工作的表格......
请查看单个调用 (JS):
function getCSVData(tableName){
var csv_value=$(tableName).table2CSV({delivery:'value'});
$("#csv_text").val(csv_value);
}
以及调用 JS 的 HTML 表单按钮(页面上有几个):
<form action="getCSV.php" method ="post" >
<input type="hidden" name="csv_text" id="csv_text">
<input type="hidden" name="fileName" id="fileName" value="<?php echo($fileName); ?>">
<input type="submit" value="Download Table 0" onclick="getCSVData('table#detail0')">
</form>
<form action="getCSV.php" method ="post" >
<input type="hidden" name="csv_text" id="csv_text">
<input type="hidden" name="fileName" id="fileName" value="<?php echo($fileName); ?>">
<input type="submit" value="Download Table 1" onclick="getCSVData('table#detail1')">
</form>
<form action="getCSV.php" method ="post" >
<input type="hidden" name="csv_text" id="csv_text">
<input type="hidden" name="fileName" id="fileName" value="<?php echo($fileName); ?>">
<input type="submit" value="Download Table 2" onclick="getCSVData('table#detail2')">
</form>
这里还有一些表格打开标签供参考:
作为参考,这里是 getCSV.php 文件
<?php
$file = $_REQUEST['fileName'];
$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
$csv_output=stripcslashes($_REQUEST['csv_text']);
print $csv_output;
exit;
?>
作为参考,这里是 table2CSV.js 代码:
jQuery.fn.table2CSV = function(options) {
var options = jQuery.extend({
separator: ',',
header: [],
delivery: 'popup' // popup, value
},
options);
var csvData = [];
var headerArr = [];
var el = this;
//header
var numCols = options.header.length;
var tmpRow = []; // construct header avalible array
if (numCols > 0) {
for (var i = 0; i < numCols; i++) {
tmpRow[tmpRow.length] = formatData(options.header[i]);
}
} else {
$(el).filter(':visible').find('th').each(function() {
if ($(this).css('display') != 'none') tmpRow[tmpRow.length] = formatData($(this).html());
});
}
row2CSV(tmpRow);
// actual data
$(el).find('tr').each(function() {
var tmpRow = [];
$(this).filter(':visible').find('td').each(function() {
if ($(this).css('display') != 'none') tmpRow[tmpRow.length] = formatData($(this).html());
});
row2CSV(tmpRow);
});
if (options.delivery == 'popup') {
var mydata = csvData.join('\n');
return popup(mydata);
} else {
var mydata = csvData.join('\n');
return mydata;
}
function row2CSV(tmpRow) {
var tmp = tmpRow.join('') // to remove any blank rows
// alert(tmp);
if (tmpRow.length > 0 && tmp != '') {
var mystr = tmpRow.join(options.separator);
csvData[csvData.length] = mystr;
}
}
function formatData(input) {
// replace " with “
var regexp = new RegExp(/["]/g);
var output = input.replace(regexp, "“");
//HTML
var regexp = new RegExp(/\<[^\<]+\>/g);
var output = output.replace(regexp, "");
if (output == "") return '';
return '"' + output + '"';
}
function popup(data) {
var generator = window.open('', 'csv', 'height=400,width=600');
generator.document.write('<html><head><title>CSV</title>');
generator.document.write('</head><body >');
generator.document.write('<textArea cols=70 rows=15 wrap="off" >');
generator.document.write(data);
generator.document.write('</textArea>');
generator.document.write('</body></html>');
generator.document.close();
return true;
}
};