连续复制
一键复制
一键打包
js_csv: {
export_csv: function (data, name) {
var uri = 'data:text/csv;charset=utf-8,\ufeff' + encodeURIComponent(data);
var downloadLink = document.createElement('a');
downloadLink.href = uri;
downloadLink.download = (name + '.csv') || 'temp.csv';
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
},
export_table: function (selector, name, split, fn) {
var self = this;
if (document.querySelectorAll(selector).length == 0) {
alert('Selector "' + selector + '" not exists!');
return false;
}
split = split ? split : null;
fn = fn ? fn : null;
self.export_csv(self.get_table_data(selector, split, fn), name);
},
get_table_data: function (selector, split, fn) {
var data = '';
var startPosition = 0, endPosition = 0;
if (split) {
startPosition = split[0];
endPosition = split[1];
}
if (fn) {
data = fn();
} else {
document.querySelectorAll(selector + ' tr').forEach(function (t, i) {
var tmp = [];
t.querySelectorAll('th,td').forEach(function (e, tdi) {
if ((tdi >= startPosition && tdi <= endPosition && endPosition > 0) || (tdi >= startPosition && endPosition === 0)) {
tmp.push(e.innerText === 'ID'?'id':e.innerText.replace('\n', '')); // 单元格里含有多个html元素可能会获取到换行符
}
});
data += tmp.join(',') + '\n';
});
}
return data;
}
},
评论已关闭