javascript - 如何在导出前从数据表中删除隐藏的单元格数据
问题描述
我正在使用 DataTable v1.10。我在 1 列中添加了一个隐藏的日期字段以对其进行正确排序。我不希望导出文件中的隐藏数据,但它显示在导出的 excel/pdf 文档中。这是我的代码。
HTML 标记:
<table id="invoices-table" class="table table-sm table-responsive-md w-100 text-nowrap">
<thead>
<tr>
<th>Invoice Number</th>
<th>Invoice Type</th>
<th>Invoice Date</th>
<th>Buyer</th>
<th>Status</th>
<th class="doNotExport"></th>
</tr>
</thead>
<tfoot>
<tr>
<th>Invoice Number</th>
<th>Invoice Type</th>
<th>Invoice Date</th>
<th>Buyer</th>
<th></th>
<th class="doNotExport"></th>
</tr>
</tfoot>
</table>
DataTable 列的 JS 代码:
columns =
[
{ data: 'invoiceNumber' },
{ data: 'invoiceType' },
// here I have added a hidden field because date was not sorting properly.
{ data: 'invoiceDate', render: function (data) { return data === null ? "" : "<span class='d-none'>" + moment(data, 'DD/MM/YYYY').format('YYYYMMDD') + "</span>" + data } },
{ data: 'dataTransferObject.BuyerDtls.LglNm' },
{ data: 'invoiceStatus' },
{
data: null,
render: function (data) {
return getButton(JSON.stringify(data));
},
},
]
在视图中它工作得很好,但是在导出到 Excel/pdf 时,如果发票日期为 02/05/2020,隐藏字段也会显示在下面的列中,然后由于该隐藏元素,它在导出的文件中显示为 2020050202/05 /2020
任何形式的帮助将不胜感激。
解决方案
在数据表上有一个名为exportOptions
. 在下面的那个集合columns: ':visible'
下。
buttons: [
{
extend: 'pdfHtml5',
exportOptions: {
columns: ':visible'
}
},
]
推荐阅读
- c - EXC_BAD_ACCESS 使用 fgets() 和 strtok() 时
- javascript - 为什么在使用 combineLatest 时不会在内部 observable 上调用运算符(点击、地图)?
- angular - 从角垫表中删除一列
- java - 尝试理解 Java 中的访问修饰符
- ruby-on-rails - Rails API + VueJS 服务器布局架构
- python - 如何构建 ARMA 或 ARIMA 模型?
- r - R如何在会话中仅显示一次消息()?
- python - Dimensionality of Keras Dense layer
- android - 带有两个选项卡的 ViewPager 中的 IndexOutOfBoundsException
- mysql - Minishift MySQL 服务未显示实际数据库