How to export all rows from Datatables using Ajax?

前端 未结 12 1202
广开言路
广开言路 2020-11-29 23:38

I am using new feature in Datatables: \"HTML5 export buttons\". I am loading data with Ajax.

https://datatables.net/extensions/buttons/examples/html5/simple.html

12条回答
  •  半阙折子戏
    2020-11-29 23:50

    If you use Laravel framework, you can use this....

    $.fn.DataTable.Api.register( 'buttons.exportData()', function( options ) {
      if(this.context.length) {
    
        var src_keyword = $('.dataTables_filter input').val();
    
        // make columns for sorting
        var columns = [];
        $.each(this.context[0].aoColumns, function(key, value) {
          columns.push({
            'data' : value.data, 
            'name' : value.name, 
            'searchable' : value.bSearchable, 
            'orderable' : value.bSortable
          });
        });
    
        // make option for sorting
        var order = [];
        $.each(this.context[0].aaSorting, function(key, value) {
          order.push({
            'column' : value[0], 
            'dir' : value[1]
          });
        });
    
        // make value for search
        var search = {
          'value' : this.context[0].oPreviousSearch.sSearch, 
          'regex' : this.context[0].oPreviousSearch.bRegex
        };
    
        var items = [];
        var status = $('#status').val();
        $.ajax({
          url: "server_side_url",
          data: { columns: columns, order: order, search: search, status: status, page: 'all' }
          success: function (result) {
    
            $.each(result.data, function(key, value) {
    
              var item = [];
    
              item.push(key+1);
              item.push(value.username);
              item.push(value.email);
              item.push(value.created_at);
              item.push(value.status);
    
              items.push(item);
            });
          },
          async: false
        });
    
        return {
          body: items, 
          // skip actions header
          header: $("#user_table thead tr th").map(function() { 
            if(this.innerHTML!='Actions')
              return this.innerHTML; 
          }).get()
        };
      }
    });
    
    var user_table = $('#user_table').DataTable({
      dom: 'Bfrtip',
      buttons: [
      'copy', 'csv', 'excel', 'pdf', 'print'
      ],
      "oSearch": {"bSmart": false},
      processing: true,
      serverSide: true,
      ajax: {
        url: "server_side_url",
        type: 'GET',
        data: function (d) {
          d.status = ""; // when onload make status as empty to get all users
        }
      },
      columns: [
      {data: 'DT_RowIndex', name: 'DT_RowIndex'},
      {data: 'username', name: 'username'},
      {data: 'email', name: 'email'},
      {data: 'created_at', name: 'created_at'},
      {data: 'status', name: 'status'},
      {data: 'actions', name: 'actions', orderable: false, searchable: false},
      ],
    });
    
    // filter users with status
    $('#status').change(function() {
      user_table.draw();
    });
    

提交回复
热议问题