[英]Download filtered html table data to csv file
I created an ejs file that can display the documents in the mongodb db collection into the table, and I added select:option
that can filtered the table. 我创建了一个ejs文件,该文件可以在表中显示mongodb db集合中的文档,并添加了
select:option
可以过滤表。 for example, select:option
of date
I choose 05/28/2019
it will only display data with 05/28/2019
date. 例如,
select:option
date
select:option
我选择05/28/2019
它只会显示日期为05/28/2019
数据。 and I already have a download form that can download the table, However the download button only download the whole data in the table and cannot download the filtered data. 并且我已经有一个可以下载表格的下载表格,但是“下载”按钮只能下载表格中的整个数据,而不能下载过滤后的数据。
My question is if there is a way that once I filtered the data in the table and I click download it will only download the filtered data not the whole table. 我的问题是,是否存在一种方法,一旦我过滤了表中的数据并单击“下载”,它将仅下载过滤后的数据,而不是整个表。
Please see the sample code here https://jsfiddle.net/indefinite/b63y928L/9/ 请在此处查看示例代码https://jsfiddle.net/indefinite/b63y928L/9/
this is the code that can filter the table 这是可以过滤表格的代码
$(document).ready(function () {
$('.filter').change(function () {
var values = [];
$('.filter').each(function () {
var colIdx = $(this).data('col');
$(this).find('option:selected').each(function () {
if ($(this).val() != "") values.push( {
text: $(this).text(),
colId : colIdx
});
});
});
filter('table > tbody > tr', values);
});
function filter(selector, values) {console.log(values);
$(selector).each(function () {
var sel = $(this);
var tokens = sel.text().trim().split('\n');
var toknesObj = [], i;
for(i=0;i<tokens.length;i++){
toknesObj[i] = {
text:tokens[i].trim(),
found:false
};
}
var show = false;
//console.log(toknesObj);
$.each(values, function (i, val) {
if (toknesObj[val.colId].text.search(new RegExp("\\b"+val.text+"\\b")) >= 0) {
toknesObj[val.colId].found = true;
}
});
console.log(toknesObj);
var count = 0;
$.each(toknesObj, function (i, val) {
if (val.found){
count+=1;
}
});
show = (count === values.length);
show ? sel.show() : sel.hide();
});
}
and this is what I followed on how to download the html table to csv file https://codepen.io/malahovks/pen/gLxLWX?editors=1010 这就是我遵循的关于如何将html表下载到csv文件https://codepen.io/malahovks/pen/gLxLWX?editors=1010的方法
I expect that once I select a date it will only download the data in the date that I selected. 我希望一旦选择了日期,它只会下载所选日期的数据。 Thank you!
谢谢!
You can modify the row selector to select only visible rows. 您可以修改行选择器以仅选择可见行。
var rows = $("table tr:visible");
. 。 Since you are already using jquery library, you can use the jquery selector.
由于您已经在使用jquery库,因此可以使用jquery选择器。
function export_table_to_csv(html, filename) {
var csv = [];
//var rows = document.querySelectorAll("table tr:visible");
var rows = $("table tr:visible");
for (var i = 0; i < rows.length; i++) {
var row = [], cols = rows[i].querySelectorAll("td, th");
for (var j = 0; j < cols.length; j++)
row.push(cols[j].innerText);
csv.push(row.join(","));
}
// Download CSV
download_csv(csv.join("\n"), filename);
}
See the updated fiddle https://jsfiddle.net/165yj7se/ 看到更新的小提琴https://jsfiddle.net/165yj7se/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.