【问题标题】:How can I make my datatable export the source data, not the generated data?如何让我的数据表导出源数据,而不是生成的数据?
【发布时间】:2020-07-05 02:42:21
【问题描述】:

好的,我有一个数据表,我正在尝试添加导出到 CSV 的功能。我似乎无法让它导出 isAuthenticated/isDisabled 列的实际值。导出时这些列的值为空白,可能是因为我使用 columndefs 在这些列中显示图像,而不是显示源数据。

我是否生成了错误的列值,或者我应该向哪个方向导出这些列的实际真/假值?

<script type="text/javascript">
    $(document).ready(function () {
        var dTable = $('#UserList').DataTable({
            "processing": true, // for show progress bar
            "serverSide": false, // for process server side
            "filter": true, // this is for disable filter (search box)
            "orderMulti": false, // for disable multiple column at once
            "pageLength": 10,
            "dom": '<"html5buttons"B>lTfgitp</div>',
            "buttons": [
                { extend: 'excel', title: 'Tool Users' }
                ],
            "ajax": {
                "url": "/Supervisor/UserData",
                "type": "POST",
                "datatype": "json"
            },
            "columns": [
                { "data": "id", "name": "id", "width": "18%" },
                { "data": "fullName", "name": "fullName", "autoWidth": true },
                { "data": "email", "name": "Email", "autoWidth": true },
                { "data": "isAuthenticated", "name": "isAuthenticated", "width":"8%" },
                { "data": "isDisabled", "name": "isDisabled", "width": "8%" },
                {
                    "render": function (data, type, full, meta)
                    { return '<a class="btn btn-info ManageUsersBtn" href="/Supervisor/EditUser/' + full.id + '">Edit</a> '; }
                }
            ],
            "columnDefs": [
                {
                    "render": function (data, type, row) {
                        return (data === true) ? '<span class="glyphicon glyphicon-ok text-navy"></span>' : '<span class=" glyphicon glyphicon-remove text-danger"></span>';
                    },
                    "targets": 3
                },
                {
                    "render": function (data, type, row) {
                        return (data === true) ? '<span class="glyphicon glyphicon-remove text-danger"></span>' : '<span class=" glyphicon glyphicon-ok text-navy"></span>';
                    },
                    "targets": 4
                }
            ],
            "fnCreatedRow": function (nRow, aData, iDataIndex) {
                if (aData.isAuthenticated) {

                }
                else {
                    $('td:eq(5)', nRow).append('<a class="btn btn-info ManageUsersBtn" onclick=AuthenticateUser("' + aData.id + '","' + aData.firstName + '","' + aData.lastName + '")>Authorize</a> ' ) ;
                }

                if (aData.isDisabled) {
                    $('td:eq(5)', nRow).append('<a class="btn btn-info ManageUsersBtn" onclick=EnableUser("' + aData.id + '","' + aData.firstName + '","' + aData.lastName + '")>Enable</a> ') ;

                }
                else {
                    $('td:eq(5)', nRow).append('<a class="btn btn-danger ManageUsersBtn" onclick=DisableUser("' + aData.id + '","' + aData.firstName + '","' + aData.lastName + '")>Disable</a> ') ;
                }
            },
        });
    });

【问题讨论】:

    标签: javascript jquery datatables export-to-csv


    【解决方案1】:

    我实际上最终弄明白了。我必须根据返回类型指定不同的数据,然后在导出选项中设置正交。它有点乱,但它有效。

    <script type="text/javascript">
        $(document).ready(function () {
            var dTable = $('#UserList').DataTable({
                "processing": true, // for show progress bar
                "serverSide": false, // for process server side
                "filter": true, // this is for disable filter (search box)
                "orderMulti": false, // for disable multiple column at once
                "pageLength": 10,
                "dom": '<"html5buttons"B>lTfgitp</div>',
                "buttons": [
                    { extend: 'excel', title: 'Tool Users', exportOptions: { orthogonal: 'export', columns: [0,1,2,3,4] } }
                    ],
                "ajax": {
                    "url": "/Supervisor/UserData",
                    "type": "POST",
                    "datatype": "json"
                },
                "columns": [
                    { "data": "id", "name": "id", "width": "18%" },
                    { "data": "fullName", "name": "fullName", "autoWidth": true },
                    { "data": "email", "name": "Email", "autoWidth": true },
                    { "data": "isAuthenticated", "name": "isAuthenticated", "width":"8%" },
                    { "data": "isDisabled", "name": "isDisabled", "width": "8%" },
                    {
                        "render": function (data, type, full, meta)
                        { return '<a class="btn btn-info ManageUsersBtn" href="/Supervisor/EditUser/' + full.id + '">Edit</a> '; }
                    }
                ],
                "columnDefs": [
                    {
                        "render": function (data, type, row) {
                            return type === 'export' ?
                                data == true ? "True" : "False" :
                                data === true ? '<span class="glyphicon glyphicon-ok text-navy"></span>' : '<span class=" glyphicon glyphicon-remove text-danger"></span>';
    
                        },
                        "targets": 3
                    },
                    {
                        "render": function (data, type, row) {
                            return type === 'export' ? 
                                data == true ? "True" : "False" : 
                                data == true ? '<span class="glyphicon glyphicon-remove text-danger"></span>' : '<span class=" glyphicon glyphicon-ok text-navy"></span>';
                        },
                        "targets": 4
                    }
                ],
                "fnCreatedRow": function (nRow, aData, iDataIndex) {
                    if (aData.isAuthenticated) {
    
                    }
                    else {
                        $('td:eq(5)', nRow).append('<a class="btn btn-info ManageUsersBtn" onclick=AuthenticateUser("' + aData.id + '","' + aData.firstName + '","' + aData.lastName + '")>Authorize</a> ' ) ;
                    }
    
                    if (aData.isDisabled) {
                        $('td:eq(5)', nRow).append('<a class="btn btn-info ManageUsersBtn" onclick=EnableUser("' + aData.id + '","' + aData.firstName + '","' + aData.lastName + '")>Enable</a> ') ;
    
                    }
                    else {
                        $('td:eq(5)', nRow).append('<a class="btn btn-danger ManageUsersBtn" onclick=DisableUser("' + aData.id + '","' + aData.firstName + '","' + aData.lastName + '")>Disable</a> ') ;
                    }
                },
            });
        });
    

    【讨论】:

      猜你喜欢
      • 2019-12-25
      • 2017-08-02
      • 1970-01-01
      • 2016-04-06
      • 2015-07-06
      • 2011-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多