Remove ‘div’ and ‘span’ on exporting dataTables to excel.

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

Remove 'div' and 'span' on exporting dataTables to excel

问题

I am trying to export an HTML table using dataTables plugins. My table has div and span tags inside td tag. While exporting the table, it is exported div and span tags as value. I tried to export only the value using the code

{extend: 'excelHtml5',
filename: 'Students with Accounts',
exportOptions: { format: { body: function (data, row, column, node ) 
{ return column === 4 ? "
{extend: 'excelHtml5',
filename: 'Students with Accounts',
exportOptions: { format: { body: function (data, row, column, node ) 
{ return column === 4 ? "\0" + data : data;
return column === 4 ? data.text() : data; } } }
},
"
+ data : data;
return column === 4 ? data.text() : data; } } } },

Still, I get the result as

Remove ‘div’ and ‘span’ on exporting dataTables to excel.

Is there any way to export only text of the cell?
Like this

Remove ‘div’ and ‘span’ on exporting dataTables to excel.

英文:

I am trying to export an HTML table using dataTables plugins. My table has div and span tags inside td tag. While exporting the table, it is exported div and span tags as value. I tried to export only the value using the code

{extend: 'excelHtml5',
filename: 'Students with Accounts',
exportOptions: { format: { body: function (data, row, column, node ) 
{ return column === 4 ? "
{extend: 'excelHtml5',
filename: 'Students with Accounts',
exportOptions: { format: { body: function (data, row, column, node ) 
{ return column === 4 ? "\0" + data : data;
return column === 4 ? data.text() : data; } } }
},
" + data : data; return column === 4 ? data.text() : data; } } } },

Still, I get the result as
Remove ‘div’ and ‘span’ on exporting dataTables to excel.

Is there any way to export only text of the cell?
Like this

Remove ‘div’ and ‘span’ on exporting dataTables to excel.

答案1

得分: 1

I think you meant to use node.innerText instead of data.text(). See innerText - and innerText has to operate on the node not on the data.


But more than that, why do you need that exportOptions option at all?

The default behavior is to use stripHtml set to true - unless you interfere with that by doing what you try to do in your example code.


The key thing to remember is this: When you use:

body: function (data, row, column, node )

The data parameter represents the entire contents inside the <td> tag - including any HTML. By using some variation of:

return data

in that function, you are overriding the stripHtml default processing - and you will get the HTML in your Excel file.

From the DataTables documentation I already linked to:

data = The cell's innerHTML
row = Cell's row index
column = Cell's column index
node = The cell node (since Buttons 1.2.2)

英文:

I think you meant to use node.innerText instead of data.text(). See innerText - and innerText has to operate on the node not on the data.


But more than that, why do you need that exportOptions option at all?

The default behavior is to use stripHtml set to true - unless you interfere with that by doing what you try to do in your example code.


The key thing to remember is this: When you use:

body: function (data, row, column, node )

The data parameter represents the entire contents inside the <td> tag - including any HTML. By using some variation of:

return data

in that function, you are overriding the stripHtml default processing - and you will get the HTML in your Excel file.

From the DataTables documentation I already linked to:

data = The cell's innerHTML
row = Cell's row index
column = Cell's column index
node = The cell node (since Buttons 1.2.2)

huangapple
  • 本文由 发表于 2023年5月22日 03:17:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76301537.html
匿名

发表评论

匿名网友

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

确定