如何让我的数据表导出源数据,而不是生成的数据?

Posted

技术标签:

【中文标题】如何让我的数据表导出源数据,而不是生成的数据?【英文标题】: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> ') ;
                
            ,
        );
    );

【问题讨论】:

【参考方案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> ') ;
                
            ,
        );
    );

【讨论】:

以上是关于如何让我的数据表导出源数据,而不是生成的数据?的主要内容,如果未能解决你的问题,请参考以下文章

如何转换 Entity Framework 6 来为我的模型生成 FluentAPI 定义,而不是数据注释? (我在 VS2015 中使用 MySQL)

如何让我的客户将 BigQuery 查询结果导出为 CSV

Flyway 或 Liquibase 可以生成更新脚本而不是直接更新数据库吗?

如何从没有绑定源 C# 生成的 datagridview 中检索数据源?

如何让我的 WPF 应用程序的多个实例使用相同的数据?

如何让我的表格正确显示分组数据?