首页 > 解决方案 > 如何在导出前从数据表中删除隐藏的单元格数据

问题描述

我正在使用 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

任何形式的帮助将不胜感激。

标签: javascriptjquerydatatablemomentjsdatatables-1.10

解决方案


在数据表上有一个名为exportOptions. 在下面的那个集合columns: ':visible'下。

buttons: [
            
            {
                extend: 'pdfHtml5',
                exportOptions: {
                    columns: ':visible'
                }
            },

        ]

推荐阅读