尝试在导出过程中从数据表中剥离HTML [英] Trying to strip html from Datatables during export
问题描述
我使用的数据表与示例完全相同此处。因此,要导出数据,我使用了一个函数,该函数允许我用excel中的换行符更改< br>
。
我用了这个:
I'm using datatables exactly as the example here. So to export data I used a function that allows me to change <br>
with newlines in excel.
I used this:
return column === 5 ? data.replace( /<br\s*\/?>/ig, "\n"): data;
但是,当我尝试添加第二个更改时,我想对其他列进行更改工作...我尝试在第一个工作替换下添加以下内容:
However when I try to add a second change I want to make to a different column it doesn't work... I tried adding this right below the first working replacement:
return column === 1 ? data.replace( /<.*?>/ig, ""): data;
我想在首先更改< br>后基本上去除所有html标签;
换行符。但是正如我现在的代码一样,第二部分在导出过程中会被忽略... 有人知道我在做什么错吗?
I want to basically strip all html tags after first changing the <br>
to newlines. But as my code is now, the second part just gets ignored during export... Does anyone know what I am doing wrong?
这里是我的完整代码:
var fixNewLine = {
exportOptions: {
format: {
body: function ( data, column, row ) {
return column === 5 ? data.replace( /<br\s*\/?>/ig, "\n"): data;
return column === 1 ? data.replace( /<.*?>/ig, ""): data;
}
}
}
};
var t2e = $('#table2excel').DataTable({
dom: 'Bfrtip',
buttons:[
$.extend( true, {}, fixNewLine, {
extend: 'excelHtml5'
} ),
'pdf', 'print', 'copy', 'csv' //, 'excel'
]
});
推荐答案
仅在代码中不能有多个return语句将执行第一个,请尝试以下操作:
You can't have multiple return statement in code only first one will be executed, try this:
var fixNewLine = {
exportOptions: {
format: {
body: function ( data, column, row ) {
if (column === 5) {
data = data.replace(/<br\s*\/?>/ig, "\n");
}
return column === 1 ? data.replace(/<.*?>/ig, ""): data;
}
}
}
};
这篇关于尝试在导出过程中从数据表中剥离HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!