jQuery Datatable – On pdf export, upon giving a new line to data, how to avoid <br/> that gets print on each new line?

huangapple go评论108阅读模式
英文:

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
&lt;br/&gt;b
&lt;br/&gt;c
&lt;br/&gt;d

我的代码如下:

exportOptions: {
        format: {
          body: function (data, column, row) {
            return column === 2 ?
              data.replace(/,/g, '\r\n') :
              data;
          }
        }
      }

我已经尝试了所有方法,但目前还没有找到解决办法。

/n /r/n &lt;br&gt; &lt;br /&gt;
英文:

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
&lt;br/&gt;b
&lt;br/&gt;c
&lt;br/&gt;d

My code looks like following:

exportOptions: {
        format: {
          body: function (data, column, row) {
            return column === 2 ?
              data.replace(/,/g, &#39;\r\n&#39;) :
              data;
          }
        }
      }

I have tried everything. So far nothing helps.

/n /r/n &lt;br&gt; &lt;br /&gt;

答案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

&lt;table id=&quot;table&quot;&gt;
&lt;thead&gt;
  &lt;tr&gt;
    &lt;th&gt;Value&lt;/th&gt;
  &lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
  &lt;tr&gt;
  &lt;td&gt;1,2,3,4&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
$(&#39;#table&#39;).DataTable({
  dom: &#39;Bfrtip&#39;,
  buttons: [{
    extend: &#39;csv&#39;,
    exportOptions: {
      format: {
        body: function(data, row, column, node) {
          return data.replaceAll(&#39;,&#39;, &#39;\r\n&#39;);
        }
      }
    }
  }]
});

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.

huangapple
  • 本文由 发表于 2023年8月8日 22:30:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76860540.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定