datatables + how to combine server side processing code with File export code

血红的双手。 提交于 2020-01-03 16:53:07

问题


This is a datatables example of adding buttons to export data to csv, pdf, excel.... fiddle here

https://datatables.net/extensions/buttons/examples/initialisation/export.html

$(document).ready(function() {
    $('#example').DataTable( {
        dom: 'Bfrtip',
        buttons: [
            'copy', 'csv', 'excel', 'pdf', 'print'
        ]
    } );
} );

This is a datatables example of Server-side processing

https://datatables.net/examples/server_side/simple.html

$(document).ready(function() {
    $('#example').DataTable( {
        "processing": true,
        "serverSide": true,
        "ajax": "scripts/server_processing.php"
    } );
} );

Now how do I combine the above code into one, so that i have a data tables that does server side processing and that this is my attempt, but I am not sure where it is wrong, or if indeed I am close.

$(document).ready(function() {
    $('#example').DataTable( {
        "processing": true,
        "serverSide": true,
        "ajax": "scripts/server_processing.php",
        "dom": 'Bfrtip',
        buttons: [
            'copy', 'csv', 'excel', 'pdf', 'print'
        ]       
    } );
} );

I have tried various permutations but I am still getting an error in the console Uncaught SyntaxError: Unexpected string Can anyone advise?

This is my real example I am working with

    $(document).ready(function() {
        var dataTable = $('#employee-grid').DataTable( {
            "processing": true,
            "serverSide": true,
            "ajax":{
                url :"employee-grid-data2.php", // json datasource
                type: "post",  // method  , by default get
                error: function(){  // error handling
                    $(".employee-grid-error").html("");
                    $("#employee-grid").append('<tbody class="employee-grid-error"><tr><th colspan="3">No data found in the server -- startagain1-index2.php </th></tr></tbody>');
                    $("#employee-grid_processing").css("display","none");

                }
            },
            "dom:" 'Bfrtip',
            "buttons": [
                'copy', 'csv', 'excel', 'pdf', 'print'
            ]
        } );
    } );

回答1:


You have the sintax error, change your code in this line:

Incorrect:

"dom:" 'Bfrtip', 

Correct:

"dom" : 'Bfrtip', 

Result: jsfiddle



来源:https://stackoverflow.com/questions/36728639/datatables-how-to-combine-server-side-processing-code-with-file-export-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!