简体   繁体   English

谁能帮我这个在Excel和PDF中下载Jquery Datatable数据

[英]Can anyone help me on this Download Jquery Datatable data in Excel and PDF

I am very new to jQuery and need to implement the data download functionality in Excel and PDF from JQuery Datatable. 我对jQuery非常陌生,需要从JQuery Datatable中实现Excel和PDF中的数据下载功能。

The Datatable is implemented already and in that only I need to implement the download data functionality. 数据表已经实现,仅我需要实现下载数据功能。

I googled it but was unable to find good articles to explain things in better perspective. 我用谷歌搜索,但是找不到更好的文章来更好地解释事物。

Below is the code for reference. 以下是供参考的代码。

HTML: HTML:

<table class="table table-bordered" data-table-name="pqe-contact-dom"></table> 

In HTML we are using data-table-name= "pqe-contact-dom" to populate the data into the table, and the datatable code is written in JS file. 在HTML中,我们使用data-table-name = “ pqe-contact-dom”将数据填充到表中,并将该数据表代码写入JS文件中。

JS File Code: JS文件代码:

var reportFunction = function(code) {   
    $.ajax({
                type : 'get',
                url : url,
                dataType : 'json',
                success : function(data) {
                var reportArray = new Array();
                    var reportArrayLen = data.length;


                    for (var l = 0; l < reportArrayLen; l++) {

                            reportArray[l] = new Array();
                            var code = data[l].CODE;
                            var name = data[l].NAME;
                            var e_name = data[l].E_Name;
                            var e_phone = data[l].E_Phone;
                            var e_email = data[l].E_eMail;


                            reportArray[l][0]= supplier_code;
                            reportArray[l][1] = supplier_name;
                            reportArray[l][2] = e_name;
                            reportArray[l][3] = e_phone;
                            reportArray[l][4] = e_email;

                        }   
                        var reportArrayVar = reportArray;
                        gotoReport( reportArrayVar ); //Calling below the function that  datatable and     //passing data array to populate data
                },
                error : function() {
                    alert("ajax error");
                }
                    });

};


//This function will populate the data to the datatable

function gotoReport(reportArrayVar){    

require( [ 'jquery', 'datatables', 'prettify', 'bootstrap'], function(jQuery) 
{ jQuery('table[data-table-name="nonqmreports-dt"]').DataTable(
/* options */{
    //useFloater : false


                useFloater : false,
                'aoColumns' : [ 
                {'sTitle' : 'Code', 'sClass' : 'essential'}, //essential
                {'sTitle' : 'Name ', 'sClass' : 'optional' },
                {'sTitle' : 'E Name', 'sClass' : 'essential'},
                {'sTitle' : 'E Phone', 'sClass' : 'essential'},
                {'sTitle' : 'E eMail ', 'sClass' : 'optional' }

                ],
                'aaData': reportArrayVar,
                'bProcessing': true,
                'isResponsive' : true,
                 'bDestroy': true,
                  'bRetrieve': false,
                'bPaginate':true,
                // 'bStateSave': true,
                 'bInfo': true,
                'sPaginationType': 'full_numbers'


})
});  

} }

In this I need to implement the data download to Excel and PDF functionality, but I am unclear how to do that. 在此,我需要将数据下载到Excel和PDF功能,但是我不清楚如何做到这一点。 Can anyone help me understand how to go about it? 谁能帮助我了解如何解决?

First things first, this should be present in your JS Code [ignore the tags in Quotes] 首先,这应该出现在您的JS代码中[忽略引号中的标签]

"dom": '<"pad5"CT><"clear"><lf<"clear">r<t>ip>',<br>
tableTools: {
  "sSwfPath": "assets/swf/copy_csv_xls_pdf.swf"
}

next you need have this file. 接下来,您需要此文件。 copy_csv_xls_pdf.swf [make sure path is according to the html/php file where is initialized] copy_csv_xls_pdf.swf [确保路径根据初始化的html / php文件而定]

next check the console f12 for errors and post the errors. 接下来,检查控制台f12中是否有错误,然后发布错误。
and mention your DataTable version and Jquery Version mine is latest 并提及您的DataTable版本,而Jquery Version我的是最新的

enjoy 请享用

First find the local path of "copy_csv_xls_pdf.swf" file inside your TableTool plugin folder and use the same path for "sSwfPath:" like I have mentioned below: 首先,在TableTool插件文件夹中找到“ copy_csv_xls_pdf.swf”文件的本地路径, 并对 “ sSwfPath:”使用相同的路径,如下所述:

require(['jquery', 'datatables', 'prettify', 'bootstrap'], function (jQuery) {
    jQuery('table[data-table-name="nonqmreports-dt"]').DataTable(
    /* options */
    {
        //useFloater : false

        useFloater: false,
            'aoColumns': [{
            'sTitle': 'Code',
            'sClass': 'essential'
        }, //essential
        {
            'sTitle': 'Name ',
            'sClass': 'optional'
        }, {
            'sTitle': 'E Name',
            'sClass': 'essential'
        }, {
            'sTitle': 'E Phone',
            'sClass': 'essential'
        }, {
            'sTitle': 'E eMail ',
            'sClass': 'optional'
        }

        ],
            'aaData': reportArrayVar,
            'bProcessing': true,
            'isResponsive': true,
            'bDestroy': true,
            'bRetrieve': false,
            'bPaginate': true,
        // 'bStateSave': true,
        'bInfo': true,
        `enter code here`
            'sPaginationType': 'full_numbers',
            "tableTools": {
            "sSwfPath": "/Scripts/TableTools-2.2.0/media/swf/copy_csv_xls_pdf.swf" // path where swf files is located.
        }
    })
});  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM