Datatables的Excel导出stripHtml不起作用。

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

Datatables excel export stripHtml doesn't work

问题

I have added a datatables export button to my datatable. In this table, some columns include html tags like:

<button data-toggle="dropdown" class="btn btn-primary dropdown-toggle btn-xs">BUTTON NAME</button>

When I first added the export button, it worked fine and the rows with HTML tags exported without their tags like: BUTTON NAME. But then I had to include a replace function to my button code as following:

buttons: [
{
extend: "excelHtml5",

exportOptions: {
  format: {
    body: function (data, row, column, node) {
      return column === 18 ? data.replace(&quot;,&quot;, &quot;.&quot;) : data;
    },
  },
},

tag: &quot;img&quot;,

className: &quot;btn-excel&quot;,

title: &quot;file_title&quot;,

titleAttr: &quot;Excel&quot;,

attr: {
  src: &quot;../images/menu_img/excel.ico&quot;,
},

action: newexportaction,

},
];

When I add this exportOptions attribute, the rows with HTML tags started to appear with their tags in the exported excel files. The column I am trying to replace doesn't have any html tag, they are all in some other column but this change affects them somehow.

I tried to add 'stripHtml: false' but it didn't make a difference. I also tried to write the replace function with switch case statements but it didn't work as well.

英文:

I have added a datatables export button to my datatable. In this table, some columns include html tags like

&lt;button data-toggle=&quot;dropdown&quot; class=&quot;btn btn-primary dropdown-toggle btn-xs&quot;&gt;BUTTON NAME&lt;/button&gt;

When I first added the export button, it worked fine and the rows with HTML tags exported without their tags like: BUTTON NAME. But then I had to include a replace function to my button code as following:

buttons: [
  {
    extend: &quot;excelHtml5&quot;,

    exportOptions: {
      format: {
        body: function (data, row, column, node) {
          return column === 18 ? data.replace(&quot;,&quot;, &quot;.&quot;) : data;
        },
      },
    },

    tag: &quot;img&quot;,

    className: &quot;btn-excel&quot;,

    title: &quot;file_title&quot;,

    titleAttr: &quot;Excel&quot;,

    attr: {
      src: &quot;../images/menu_img/excel.ico&quot;,
    },

    action: newexportaction,
  },
];

When I add this exportOptions attribute, the rows with HTML tags started to appear with their tags in the exported excel files. The column I am trying to replace doesn't have any html tag, they are all in some other column but this change affects them somehow.

I tried to add 'stripHtml: false' but it didn't make a difference. I also tried to write the replace function with switch case statements but it didn't work as well.

答案1

得分: 1

以下是您要翻译的内容:

"When you use the format function for your Excel export button, DataTables populates its data parameter with the raw data from each DataTable cell. So, that raw data is the full HTML content for your BUTTON NAME button.

More precisely, you get each cell's innerHtml. See also here.

That is why some other options such as stripHtml: true no longer have any effect.

You can perform the equivalent stripHtml step (in your specific case) by using the node parameter instead of the data parameter - and accessing the node's innerText using JavaScript:

body: function (data, row, column, node) {
  if (column === 5) {
    return data.replace(&quot;,&quot;, &quot;.&quot;);
  } else if ( column === 3) {
    return node.innerText;
  } else {
    return data;
  }
}

My output in Excel is:

Datatables的Excel导出stripHtml不起作用。


Here is my full test example, so you can see the source data:

<!doctype html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Demo</title>
  <link href="https://cdn.datatables.net/1.13.4/css/jquery.dataTables.css" rel="stylesheet"/>
  <link href="https://cdn.datatables.net/buttons/2.3.6/css/buttons.dataTables.css" rel="stylesheet"/>
 
  <script src="https://code.jquery.com/jquery-3.6.0.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/pdfmake.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/vfs_fonts.js"></script>
  <script src="https://cdn.datatables.net/1.13.4/js/jquery.dataTables.js"></script>
  <script src="https://cdn.datatables.net/buttons/2.3.6/js/dataTables.buttons.js"></script>
  <script src="https://cdn.datatables.net/buttons/2.3.6/js/buttons.html5.js"></script>

  <link rel="stylesheet" type="text/css" href="https://datatables.net/media/css/site-examples.css">

</head>

<body>

<div style="margin: 20px;">

    <table id="example" class="display dataTable cell-border" style="width:100%">
        <thead>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Office in Country</th>
                <th>Age</th>
                <th>Start date</th>
                <th>Salary</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Tiger Nixon</td>
                <td>System Architect</td>
                <td>Edinburgh</td>
                <td><button data-toggle="dropdown" class="btn btn-primary dropdown-toggle btn-xs">BUTTON NAME</button></td>
                <td>2011/04/25</td>
                <td>$320,800</td>
            </tr>
            <tr>
                <td>Garrett Winters</td>
                <td>Accountant</td>
                <td>Tokyo</td>
                <td><button data-toggle="dropdown" class="btn btn-primary dropdown-toggle btn-xs">BUTTON NAME</button></td>
                <td>2011/07/25</td>
                <td>$170,750</td>
            </tr>
        </tbody>
    </table>

</div>

<script>

$(document).ready(function() {

  $('#example').DataTable( {
    dom: 'Bfrtip',
    buttons: [
      {
        extend: 'excelHtml5',
        exportOptions: {
          stripHtml: true,
          format: {
            body: function (data, row, column, node) {
              if (column === 5) {
                return data.replace(",",".");
              } else if ( column === 3) {
                return node.innerText;
              } else {
                return data;
              }
            }
          }
        }
      }
    ]

  } );

} );

</script>

</body>
</html>
英文:

When you use the format function for your Excel export button, DataTables populates its data parameter with the raw data from each DataTable cell. So, that raw data is the full HTML content for your BUTTON NAME button.

More precisely, you get each cell's innerHtml. See also here.

That is why some other options such as stripHtml: true no longer have any effect.

You can perform the equivalent stripHtml step (in your specific case) by using the node parameter instead of the data parameter - and accessing the node's innerText using JavaScript:

body: function (data, row, column, node) {
  if (column === 5) {
    return data.replace(&quot;,&quot;, &quot;.&quot;);
  } else if ( column === 3) {
    return node.innerText;
  } else {
    return data;
  }
}

My output in Excel is:

Datatables的Excel导出stripHtml不起作用。


Here is my full test example, so you can see the source data:

&lt;!doctype html&gt;
&lt;html&gt;
&lt;head&gt;
  &lt;meta charset=&quot;UTF-8&quot;&gt;
  &lt;title&gt;Demo&lt;/title&gt;
  &lt;link href=&quot;https://cdn.datatables.net/1.13.4/css/jquery.dataTables.css&quot; rel=&quot;stylesheet&quot;/&gt;
  &lt;link href=&quot;https://cdn.datatables.net/buttons/2.3.6/css/buttons.dataTables.css&quot; rel=&quot;stylesheet&quot;/&gt;
 
  &lt;script src=&quot;https://code.jquery.com/jquery-3.6.0.js&quot;&gt;&lt;/script&gt;
  &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.js&quot;&gt;&lt;/script&gt;
  &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/pdfmake.js&quot;&gt;&lt;/script&gt;
  &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/vfs_fonts.js&quot;&gt;&lt;/script&gt;
  &lt;script src=&quot;https://cdn.datatables.net/1.13.4/js/jquery.dataTables.js&quot;&gt;&lt;/script&gt;
  &lt;script src=&quot;https://cdn.datatables.net/buttons/2.3.6/js/dataTables.buttons.js&quot;&gt;&lt;/script&gt;
  &lt;script src=&quot;https://cdn.datatables.net/buttons/2.3.6/js/buttons.html5.js&quot;&gt;&lt;/script&gt;

  &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;https://datatables.net/media/css/site-examples.css&quot;&gt;

&lt;/head&gt;

&lt;body&gt;

&lt;div style=&quot;margin: 20px;&quot;&gt;

    &lt;table id=&quot;example&quot; class=&quot;display dataTable cell-border&quot; style=&quot;width:100%&quot;&gt;
        &lt;thead&gt;
            &lt;tr&gt;
                &lt;th&gt;Name&lt;/th&gt;
                &lt;th&gt;Position&lt;/th&gt;
                &lt;th&gt;Office in Country&lt;/th&gt;
                &lt;th&gt;Age&lt;/th&gt;
                &lt;th&gt;Start date&lt;/th&gt;
                &lt;th&gt;Salary&lt;/th&gt;
            &lt;/tr&gt;
        &lt;/thead&gt;
        &lt;tbody&gt;
            &lt;tr&gt;
                &lt;td&gt;Tiger Nixon&lt;/td&gt;
                &lt;td&gt;System Architect&lt;/td&gt;
                &lt;td&gt;Edinburgh&lt;/td&gt;
                &lt;td&gt;&lt;button data-toggle=&quot;dropdown&quot; class=&quot;btn btn-primary dropdown-toggle btn-xs&quot;&gt;BUTTON NAME&lt;/button&gt;&lt;/td&gt;
                &lt;td&gt;2011/04/25&lt;/td&gt;
                &lt;td&gt;$320,800&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
                &lt;td&gt;Garrett Winters&lt;/td&gt;
                &lt;td&gt;Accountant&lt;/td&gt;
                &lt;td&gt;Tokyo&lt;/td&gt;
                &lt;td&gt;&lt;button data-toggle=&quot;dropdown&quot; class=&quot;btn btn-primary dropdown-toggle btn-xs&quot;&gt;BUTTON NAME&lt;/button&gt;&lt;/td&gt;
                &lt;td&gt;2011/07/25&lt;/td&gt;
                &lt;td&gt;$170,750&lt;/td&gt;
            &lt;/tr&gt;
        &lt;/tbody&gt;
    &lt;/table&gt;

&lt;/div&gt;

&lt;script&gt;

$(document).ready(function() {

  $(&#39;#example&#39;).DataTable( {
    dom: &#39;Bfrtip&#39;,
    buttons: [
      {
        extend: &#39;excelHtml5&#39;,
        exportOptions: {
          stripHtml: true,
          format: {
            body: function (data, row, column, node) {
              if (column === 5) {
                return data.replace(&quot;,&quot;, &quot;.&quot;);
              } else if ( column === 3) {
                return node.innerText;
              } else {
                return data;
              }
            }
          }
        }
      }
    ]

  } );

} );

&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;

huangapple
  • 本文由 发表于 2023年4月13日 18:04:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76004170.html
匿名

发表评论

匿名网友

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

确定