从服务器端 yajra 数据表下载所有记录的问题

问题描述

我想从服务器端 yajra 数据表下载所有记录为 pdf 和 excel 格式,而不仅仅是我在下面的代码中制作的特定页面

var table = $('.dataTables-example').DataTable({
    "aaSorting": [],destroy: true,pageLength: 25,responsive: true,// processing: true,serverSide: true,dom: '<"html5buttons"B>lTftigp',buttons: 
    [
        { 
            extend: 'copy',footer: true
        },{
            extend: 'csv',{
            extend: 'excel',title: 'Customer Sales Rep Sales Totals',footer: true,action: newExportAction
        },{ 
            extend: 'pdfHtml5',orientation: 'landscape',exportOptions: 
            {
                columns: ':visible',search: 'applied',order: 'applied',modifier: 
                {
                    pageMargins: [ 0,0 ],// try #3 setting margins
                    margin: [ 10,// try #4 setting margins
                    alignment: 'center'
                },body: 
                {
                    margin:[10,0],pageMargins: [ 0,alignment: 'center'
                },columns: [0,1,2,3,4,5,6,7] //column id visible in PDF    
            },customize: function (doc) 
            {                       
                //doc.content.splice(0,1);
                doc.content[1].table.widths = ['10%','10%','15%','15%'];
                doc.content[1].borders = ['2px solid'];

                /* doc.content[1].table.widths =
                                Array(doc.content[1].table.body[1].length + 1).join('*').split('');*/

                doc.content.splice(0,0);

                //Create a date string that we use in the footer. Format is dd-mm-yyyy
                var now = new Date();
                var jsDate = now.getDate()+'-'+(now.getMonth()+1)+'-'+now.getFullYear();
                
                var logo = 'logo.png';
                
                doc.pageMargins = [20,60,20,30];

                // Set the font size fot the entire document
                doc.defaultStyle.fontSize = 8;
                /*doc.defaultStyle.width=100;*/
                // Set the fontsize for the table header
                doc.styles.tableHeader.fontSize = 10;
                doc.styles.tableHeader.alignment = 'left';
                
                doc['header']=(function() {
                    return {
                        columns: [
                            {
                                image: logo,width: 80
                            },],margin: 20,width:100
                    }
                });
            
                doc['footer']=(function(page,pages) {
                    return {
                        columns: [
                            {
                                alignment: 'left',text: ['Created on: ',{ text: jsDate.toString() }]
                            },{
                                alignment: 'right',text: ['page ',{ text: page.toString() },' of ',{ text: pages.toString() }]
                            }
                        ],margin: 20
                    }
                });
            
                var objLayout = {};
                objLayout['hLineWidth'] = function(i) { return .100; };
                objLayout['vLineWidth'] = function(i) { return .100; };
                objLayout['hLineColor'] = function(i) { return '#aaa'; };
                objLayout['vLineColor'] = function(i) { return '#aaa'; };
                objLayout['paddingLeft'] = function(i) { return 0; };
                objLayout['paddingRight'] = function(i) { return 0; };
                objLayout['alignment'] = function(i) { return 'center'; };
                doc.content[0].layout = objLayout;
            }
        },{
            extend: 'print',text : 'Print Preview',exportOptions: 
            {
                columns: ':not(.no-export)'
            },customize: function (win)
            {
                $(win.document.body).addClass('white-bg');
                $(win.document.body).css('font-size','10px');
        
                $(win.document.body).find('table')
                        .addClass('compact')
                        .css('font-size','inherit');

                $(win.document.body).find('image')
                        .addClass('img')
                        .css('height','50');                                               
            }
        }
    ],ajax: {
        url:'{{ route("admin.ajaxCustomerSalesRepTotal") }}',method:'post',data: { srch_start_date: srch_start_date,srch_end_date : srch_end_date },},columns : [
        {data : 'total_sales',name : 'total_sales'},{data : 'total_orders',name : 'total_orders'},{data : 'account_number',name : 'account_number'},{data : 'account_suffix',name : 'account_suffix'},{data : 'sales_rep_id',name : 'sales_rep_id'},{data : 'sales_last_name',name : 'sales_last_name'},{data : 'sales_first_name',name : 'sales_first_name'},{data : 'sales_state_name',name : 'sales_state_name'},});

并根据 This 链接,我使用以下代码以 excel 格式实现此功能

var oldExportAction = function (self,e,dt,button,config) {
    if (button[0].className.indexOf('buttons-excel') >= 0) {
        if ($.fn.dataTable.ext.buttons.excelHtml5.available(dt,config)) {
            $.fn.dataTable.ext.buttons.excelHtml5.action.call(self,config);
        }
        else {
            $.fn.dataTable.ext.buttons.excelFlash.action.call(self,config);
        }
    } else if (button[0].className.indexOf('buttons-print') >= 0) {
        $.fn.dataTable.ext.buttons.print.action(e,config);
    }
};

var newExportAction = function (e,config) {
    var self = this;
    var oldStart = dt.settings()[0]._iDisplayStart;

    dt.one('preXhr',function (e,s,data) {
        // Just this once,load all data from the server...
        data.start = 0;
        data.length = 2147483647;

        dt.one('preDraw',settings) {
            // Call the original action function 
            oldExportAction(self,config);

            dt.one('preXhr',data) {
                // DataTables thinks the first item displayed is index 0,but we're not drawing that.
                // Set the property to what it was before exporting.
                settings._iDisplayStart = oldStart;
                data.start = oldStart;
            });

            // Reload the grid with the original page. Otherwise,API functions like table.cell(this) don't work properly.
            setTimeout(dt.ajax.reload,0);

            // Prevent rendering of the full data to the DOM
            return false;
        });
    });

    // Requery the server with the new one-time export settings
    dt.ajax.reload();
};

但我无法为 PDF 搜索此类型的功能,任何人都可以帮助我

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...