首页 > 解决方案 > jquery datatable如何在导出到excel pdf时从标题中删除下拉过滤器选择值

问题描述

我在我的数据表上使用自定义过滤器选择框,并尝试实现导出按钮。但是,我从选择中显示为导出的标题。我想在导出到 excel pdf 或打印时从标题中删除下拉过滤器选择值,这是我的代码,我还附上了输出的屏幕截图我用来生成选择过滤器的代码是:

附上截图

 $(document).ready(function() {
        $('#invoicetable').DataTable( {
            dom: 'lBfrtip',
            buttons: [
        { extend: 'print', filename: 'Pay Summary',title: 'Pay Summary',footer: true },
        { extend: 'excelHtml5', filename: 'Pay Summary',title: 'Pay Summary',footer: true },
        { extend: 'pdfHtml5', filename: 'Pay Summary', title: 'Pay Summary', footer: true },
    ],
            initComplete: function () {
                this.api().columns([0,2]).every( function () {
                    var column = this;
                    var select = $('<select><option value=""></option></select>')
                        .appendTo( $(column.header()).empty() )
                        .on( 'change', function () {
                            var val = $.fn.dataTable.util.escapeRegex(
                                $(this).val()
                            );

                            column
                                .search( val ? '^'+val+'$' : '', true, false )
                                .draw();
                        } );

                    column.data().unique().sort().each( function ( d, j ) {
                        select.append( '<option value="'+d+'">'+d+'</option>' )
                    } );
                } );
            },

            "footerCallback": function ( row, data, start, end, display ) {
        var totapi = this.api(), data;

        // Remove the formatting to get integer data for summation
        var intVal = function ( i ) {
            return typeof i === 'string' ?
                i.replace(/[\$,]/g, '')*1 :
                typeof i === 'number' ?
                    i : 0;
        };
        pageTotal = totapi
            .column( 3, { page: 'current'} )
            .data()
            .reduce( function (a, b) {
                return intVal(a) + intVal(b);
            }, 0 );

            saltot = totapi
            .column( 5, { page: 'current'} )
            .data()
            .reduce( function (a, b) {
                return intVal(a) + intVal(b);
            }, 0 );

            dectot = totapi
            .column( 6, { page: 'current'} )
            .data()
            .reduce( function (a, b) {
                return intVal(a) + intVal(b);
            }, 0 );

            salpay = totapi
            .column( 7, { page: 'current'} )
            .data()
            .reduce( function (a, b) {
                return intVal(a) + intVal(b);
            }, 0 );

        // Update footer
        $( totapi.column( 3 ).footer() ).html(pageTotal);
        $( totapi.column( 5 ).footer() ).html(saltot);
        $( totapi.column( 6 ).footer() ).html(dectot);
        $( totapi.column( 7 ).footer() ).html(salpay);
    }

        } );

    } );

标签: jquerydatatables

解决方案


您可以使用,

exportOptions: { 
            format: {
                header: function ( data, column, row )
                    {
                        return data.substring(data.indexOf("value")+9,data.indexOf("</option"));
                    }
             }
        },

它为我工作


推荐阅读