英文:
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
Is there any way to export only text of the cell?
Like this
英文:
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; } } }
},
Is there any way to export only text of the cell?
Like this
答案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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论