4
votes

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
don't think this is a duplicate?HattrickNZ

1 Answers

3
votes

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

Incorrect:

"dom:" 'Bfrtip', 

Correct:

"dom" : 'Bfrtip', 

Result: jsfiddle