英文:
jQuery Datatable - On pdf export, upon giving a new line to data, how to avoid <br/> that gets print on each new line?
问题
我有一个数据表格,其中有很多数据。我有两个按钮用于导出PDF和CSV文件。其中一个列中有一个逗号分隔的字符串。我希望在导出PDF时将其拆分并显示为新行。
例如:data = a, b, c, d;
期望的PDF和CSV输出为:
a
b
c
d
当前的输出为:
a
<br/>b
<br/>c
<br/>d
我的代码如下:
exportOptions: {
format: {
body: function (data, column, row) {
return column === 2 ?
data.replace(/,/g, '\r\n') :
data;
}
}
}
我已经尝试了所有方法,但目前还没有找到解决办法。
/n /r/n <br> <br />
英文:
I have a datatable which have bunch of data. I have 2 buttons for PDF and CSV. I have a comma seperated string in one of the column. I want to split and display with a new line when I export the PDF.
Eg: data = a, b, c, d;
Expected output on PDF and CSV
a
b
c
d
Current Output:
a
<br/>b
<br/>c
<br/>d
My code looks like following:
exportOptions: {
format: {
body: function (data, column, row) {
return column === 2 ?
data.replace(/,/g, '\r\n') :
data;
}
}
}
I have tried everything. So far nothing helps.
/n /r/n <br> <br />
答案1
得分: 1
这是一个非常基本的工作示例,展示了你想要做的事情:
<table id="table">
<thead>
<tr>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>1,2,3,4</td>
</tr>
</tbody>
</table>
$('#table').DataTable({
dom: 'Bfrtip',
buttons: [{
extend: 'csv',
exportOptions: {
format: {
body: function(data, row, column, node) {
return data.replaceAll(',', '\r\n');
}
}
}
}]
});
你可以在这里尝试它。
这与你尝试做的事情没有太大区别(除了可能使用更简单的replaceAll
而不是replace
)。这并不能解释你的代码有什么问题,但它可能会帮助你修复它。
英文:
Here is a very basic working example of what you're trying to do
<table id="table">
<thead>
<tr>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>1,2,3,4</td>
</tr>
</tbody>
</table>
$('#table').DataTable({
dom: 'Bfrtip',
buttons: [{
extend: 'csv',
exportOptions: {
format: {
body: function(data, row, column, node) {
return data.replaceAll(',', '\r\n');
}
}
}
}]
});
You can try it here
This is not much different from what you're trying to do (except maybe for the use of the easier replaceAll
instead of replace
). That does not explain what's wrong with your code but it may help you fix it anyway.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论