打印数据表中的所有数据

时间:2016-07-12 21:39:42

标签: javascript jquery asp.net-mvc

我正在使用asp.net mvc。当数据表列表时,发送动作和响应只有分页数据。如果点击打印按钮,我想打印所有数据。

var table = $('#dataTableNoticeReport');
var oTable = table.dataTable({
    "bScrollCollapse": true,
    "bDestroy": true,
    "paging": true,
    "ordering": false,
    "info": true,
    "bFilter": true,
    "bProcessing": true,
    "bLengthChange": true,
    "bServerSide": true,
    "sAjaxSource": "/Manage/Notice/_NoticeDatatable?readStatus=" + $(this).val() + "&noticeId=" + $("#NoticeSearch_Id").val(),
    "sServerMethod": "POST",
    "aoColumns": [{
        "mDataProp": "Name"
    }, {
        "mDataProp": "SurName"
    }, {
        "mDataProp": "PhoneNumber"
    }, ],
    // Internationalisation. For more info refer to http://datatables.net/manual/i18n
    "language": {
        "aria": {
            "sortAscending": ": activate to sort column ascending",
            "sortDescending": ": activate to sort column descending"
        },
        "emptyTable": "Tabloda Gösterilecek Kayıt Bulunamadı",
        "info": "Gösterilen : _START_ - _END_  /  _TOTAL_ ",
        "infoEmpty": "Kayıt Bulunamadı.",
        "infoFiltered": "(Toplam _MAX_ kayıttan sorgulama yapıldı.)",
        "lengthMenu": "_MENU_  Kayıt Sayısı",
        "search": "Ara:",
        "zeroRecords": "Kayıt Bulunamadı."
    },

    // Or you can use remote translation file
    //"language": {
    //   url: '//cdn.datatables.net/plug-ins/3cfcc339e89/i18n/Portuguese.json'
    //},
    dom: 'Bfrtip',
    buttons: [{
            extend: 'print',
            "sButtonText": 'Export',
            className: 'btn default',
            oSelectorOpts: {
                page: 'all'
            },
            bShowAll: true,
            sAjaxSource: "/Manage/Notice/_NoticeDatatable?readStatus=" + $(this).val() + "&noticeId=" + $("#NoticeSearch_Id").val()


        }, {
            extend: 'copy',
            className: 'btn default'
        }, {
            extend: 'pdf',
            className: 'btn default'
        }, {
            extend: 'excel',
            className: 'btn default'
        }, {
            extend: 'csv',
            className: 'btn default'
        },

    ],

    "order": [
        [0, 'asc']
    ],

    "lengthMenu": [
        [5, 10, 15, 20, 5000],
        [5, 10, 15, 20, "All"] // change per page values here
    ],
    // set the initial value
    "pageLength": 10,


});

它无法在代码下工作。

 buttons: [{
         extend: 'print',
         "sButtonText": 'Export',
         className: 'btn default',
         oSelectorOpts: {
             page: 'all'
         },
         bShowAll: true,
         sAjaxSource: "/Manage/Notice/_NoticeDatatable?readStatus=" + $(this).val() + "&noticeId=" + $("#NoticeSearch_Id").val()


     }, {
         extend: 'copy',
         className: 'btn default'
     }, {
         extend: 'pdf',
         className: 'btn default'
     }, {
         extend: 'excel',
         className: 'btn default'
     }, {
         extend: 'csv',
         className: 'btn default'
     },

 ],

当我点击打印按钮时,只返回10条记录,但我想要全部记录打印。

2 个答案:

答案 0 :(得分:1)

我也在使用datatables服务器端,这就是我设法打印所有数据而不制动任何东西的方法:提供“lengthMenu”选项让用户选择所有数据:

    $('#table').DataTable({
            "ajax": {
                url: "my_url",
                type: 'POST'
            },
            "lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
            "processing": true,
            "serverSide": true,
            "bPaginate": true,
            "sPaginationType": "full_numbers",
            "columns": [
                { "data": "a" },
                { "data": "b" },
                { "data": "c" },
                { "data": "d" },
                { "data": "e" }
            ],
            "dom": 'Blfrtip',
            "buttons": [
                { 
                    extend: 'print'
                }
            ]
        });

    });

这里,通过提供“lengthMenu”选项,用户可以选择要查看的范围或“全部”数据,如果用户想要导出所有数据,则用户将从长度菜单中选择“全部”选项下降。

答案 1 :(得分:0)

认为当启用服务器端时,您需要返回记录数。请参阅documentation,尤其是ajax部分。请注意JSON响应中的记录总数:

  "recordsTotal": 57,

这是一个很好的Datatables支持线程:https://datatables.net/forums/discussion/32031/pagination-with-server-side-processing。他们详细讨论了服务器端分页。