1
代碼與價值創造裏面CSV文件:如何延遲功能,持續10秒
function exportTableToCSV($table, filename) {
alert(filename);
var $rows = $table.find('tr:has(td)'),
// Temporary delimiter characters unlikely to be typed by keyboard
// This is to avoid accidentally splitting the actual contents
tmpColDelim = String.fromCharCode(11), // vertical tab character
tmpRowDelim = String.fromCharCode(0), // null character
// actual delimiter characters for CSV format
colDelim = '","',
rowDelim = '"\r\n"',
// Grab text from table into CSV formatted string
csv = '"' + $rows.map(function (i, row) {
var $row = $(row),
$cols = $row.find('td');
return $cols.map(function (j, col) {
var $col = $(col),
text = $col.text();
return text.replace(/"/g, '""'); // escape double quotes
}).get().join(tmpColDelim);
}).get().join(tmpRowDelim)
.split(tmpRowDelim).join(rowDelim)
.split(tmpColDelim).join(colDelim) + '"',
// Data URI
csvData = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csv);
alert(csvData);
$(this)
.attr({
'download': filename,
'href': csvData,
'target': '_blank'
});
}
下面的代碼是點擊按鈕,將數據導出到CSV文件後使用。
$(".Approve").on('click', function (event) {
var that = this; // need to capture `this` context, it doesn't carry into the timer context
setTimeout(function() {
exportTableToCSV.apply(that, [ $('#myDiv>table'), 'XYZ.csv' ]);
}, 10 * 100);
});
});
我申請的變化,現在超時工作,但XYZ.csv不能與數據下載。
他'this' – MinusFour
它不工作 –
@MinusFour這將使感後失蹤一個逗號。 Moubani可以更具描述性嗎?我無法讀懂頭腦。 – Halcyon