我正在使用此指令将html表导出到csv:
myApp.directive('exportToCsv', function () {
return {
restrict: 'A',
link: function (scope, element, attrs) {
function stringify(str) {
return '"' + str.replace(/^\s\s*/, '').replace(/\s*\s$/, '').replace(/"/g, '""') + '"';
}
element.bind('click', function (e) {
var table = document.querySelector("#targetTable");
/* var table = e.target.nextElementSibling;*/
var csvString = '';
for (var i = 0; i < table.rows.length; i++) {
var rowData = table.rows[i].cells;
for (var j = 1; j < rowData.length; j++) {
if (rowData[j].innerHTML.indexOf('<a href') > -1) {
var ele = angular.element(rowData[j]);
for (var k = 0; k < ele[0].childNodes.length; k++) {
if (ele[0].childNodes[k].tagName == 'A') {
csvString = csvString + stringify(ele[0].childNodes[k].innerText) + ",";
}
}
} else if (rowData[j].innerHTML.indexOf('<em') > -1 || rowData[j].innerHTML.indexOf('<strong>') > -1) {
var ele = angular.element(rowData[j]);
csvString = csvString + stringify(ele[0].textContent) + ",";
} else {
csvString = csvString + stringify(rowData[j].innerHTML) + ",";
}
}
csvString = csvString.substring(0, csvString.length - 1);
csvString = csvString + "\n";
}
console.log(csvString);
csvString = csvString.substring(0, csvString.length - 1);
var dl = new Blob([csvString], { type: 'application/csv;base64' });
saveAs(dl, "export.csv")
});
}
}
});
在这段代码中真正困扰我的是“var table = document.querySelector(”#targetTable“);”因为它不允许我在同一个html视图上重复使用该指令两次。
我试图想象如何将目标id作为参数传递给指令函数,所以我可以在这样的标记中使用它:
<button export-to-csv type="button" id-parameter="targetTable1" style="background-color: Transparent; color:green">Export:CSV</button >
<button export-to-csv type="button" id-parameter="targetTable2" style="background-color: Transparent; color:green">Export:CSV</button >
Angularjs指令可以吗?
答案 0 :(得分:1)
根据我对你的问题的理解,你想在指令中传递一个值:
myApp.directive('exportToCsv', function () {
return {
restrict: 'A',
scope : {
idParameter : '@' // pass value to the directive
},
link: function (scope, element, attrs) {
function stringify(str) {
return '"' + str.replace(/^\s\s*/, '').replace(/\s*\s$/, '').replace(/"/g, '""') + '"';
}
element.bind('click', function (e) {
var uniqueId = '#'+scope.idParameter; // <--- you can use it here
var table = document.querySelector(uniqueId);
/* var table = e.target.nextElementSibling;*/
var csvString = '';
for (var i = 0; i < table.rows.length; i++) {
var rowData = table.rows[i].cells;
for (var j = 1; j < rowData.length; j++) {
if (rowData[j].innerHTML.indexOf('<a href') > -1) {
var ele = angular.element(rowData[j]);
for (var k = 0; k < ele[0].childNodes.length; k++) {
if (ele[0].childNodes[k].tagName == 'A') {
csvString = csvString + stringify(ele[0].childNodes[k].innerText) + ",";
}
}
} else if (rowData[j].innerHTML.indexOf('<em') > -1 || rowData[j].innerHTML.indexOf('<strong>') > -1) {
var ele = angular.element(rowData[j]);
csvString = csvString + stringify(ele[0].textContent) + ",";
} else {
csvString = csvString + stringify(rowData[j].innerHTML) + ",";
}
}
csvString = csvString.substring(0, csvString.length - 1);
csvString = csvString + "\n";
}
console.log(csvString);
csvString = csvString.substring(0, csvString.length - 1);
var dl = new Blob([csvString], { type: 'application/csv;base64' });
saveAs(dl, "export.csv")
});
}
}
});
如果您有更多疑问,请在评论中告诉我。