Exporting all rows to CSV, instead of what is just displayed on page.

Exporting all rows to CSV, instead of what is just displayed on page.

wyattbikerwyattbiker Posts: 25Questions: 14Answers: 0

Here is the code. Works great except the export will not export all. Note: It uses server side, so i am probably missing something.

$(document).ready(function() {
    $('#users').DataTable( {
        "processing": true,
        "serverSide": true,
        "dom": 'Blfrtip',
        'pageLength':10,
        "buttons": [
            {
                extend: 'csv',
                text : 'Export to CSV',
                exportOptions : {
                    modifier : {
                        // DataTables core
                        order : 'current',  // 'current', 'applied', 'index',  'original'
                        page : 'all',      // 'all',     'current'
                        search : 'applied'     // 'none',    'applied', 'removed'
                    }
                }
            }        
        ],
        "ajax": {
            "url":      "cgi-bin/businessview.pl",
            "dataType": "json",
            "data": function ( d ) {
                x=2;
            },
            "dataSrc": function ( json ) {
              for ( var i=0, ien=json.data.length ; i<ien ; i++ ) {
                json.data[i][0] = '<a href=#>'+json.data[i][0]+'</a>';
              }
              return json.data;
            }                    
            // Never use success with Datatables, instead use dataSrc
            // "success": function(json){
            //     return json;
            // }
        
         }
    } );
});

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,563Questions: 26Answers: 4,995

    I've never used server side processing but there is a FAQ for this question:
    https://datatables.net/faqs/#Server-side-processing

    Kevin

  • wyattbikerwyattbiker Posts: 25Questions: 14Answers: 0

    That's what I thought. Serverside will not work. Can I trap the Button press and change the request to the server (ie length to -1 for all records). Then use 'dataSrc' to export what comes back without touching the current table on the screen?

    Thanks!

  • allanallan Posts: 63,839Questions: 1Answers: 10,518 Site admin
    Answer ✓

    But if you send the length to -1 then you are loosing any benefit you might have been gaining by using server-side processing. Indeed, at that point the only thing server-side processing would be adding is network latency to your application!

    You'll also find that client-side export is really quite slow for large data sets (for which server-side processing is designed for).

    I would really not recommend using that method :smile:.

    Allan

This discussion has been closed.