[datatables -- 学习02]: datatables 导出 CSV和excel 文件,甚至copy内容

前端代码:

<!DOCTYPE HTML>
<html>
<head>
    <base href="/"/>
    <meta charset="utf-8">
    <title>Fault Feature details</title>

<link rel="stylesheet" href="css/jquery.dataTables.min.css">
<link rel="stylesheet" href="css/buttons.dataTables.min.css">
<link rel="stylesheet" href="css/dataTables.colVis.min.css">

<script src="lib/jquery-1.11.3.min.js"></script>

<!--<link href="css/bootstrap.min.css" rel="stylesheet">--> 
<script src="lib/moment.min.js"></script>
<script src="lib/lodash.min.js"></script>
<script src="lib/jquery.dataTables.min.js"></script>    
<script src="lib/dataTables.buttons.min.js"></script>
<script src="lib/buttons.colVis.min.js"></script>
<script src="lib/buttons.flash.min.js"></script>
<script src="lib/buttons.html5.min.js"></script>
<script src="lib/buttons.print.min.js"></script>
<script src="lib/dataTables.colVis.min.js"></script>
<script src="js/settings.js"></script>
<script src="js/utils.js"></script>


</head>

<body>

        <div class="section">
            <div class="container">
                <div>
                    <div class="content" id="header">

                    </div>
                    <table id="example" class="display" cellspacing="0" width="100%" style="white-space: nowrap;text-overflow: ellipsis;">

                        <thead>
                        <tr>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>

                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>

                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>

                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>

                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                        </tr>
                        </thead>
                        <tbody></tbody>
                    </table>
                </div>
            </div>
        </div>





</body>
<script src="js/fault/fault_feature_details.js"></script> //我的JS文件
<script type="text/javascript">
    var link = window.location.href;
    var featureType = link.replace(/.*fetOpts=(.*?)&.*/, "$1").replace("_", " ").replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});

    if (window.location.href.indexOf("New_Feature") != -1) {
        $('#header').append('<h2>'+ featureType + '\'s Pronto details of New Feature</h2>');

    }else if (window.location.href.indexOf("Legacy") != -1) {
        $('#header').append('<h2>'+featureType+'\'s Pronto details of Legacy</h2>');

    } else if (window.location.href.indexOf("NA") != -1){
        $('#header').append('<h2>'+featureType+'\'s Pronto details of NA</h2>');
    }
    draw_detail_table();

</script>
</html>

JS代码:fault_feature_details.js

'use strict';

var multiselectButtons = ['interval','productLineOpts', 'productOpts', 'systemOpts', 'entityOpts',
                          'buBlOpts', 'orgCatOpts','planeGroupedOpts', 'planeOpts', 'level4DuOpts',
                          'componentOpts',
                          'severityOpts', 'typeOpts', 'stateOpts', 'customerPrOpts', 
                          'reportTypeOpts', 'removeDuplicateOpts', 'goalOpts', 
                          'targetOpts', 'cnaOpts', 'monshoOpts', 'fetOpts'];

function getParamsFromUrl() {
    var params = {};
    params['begin'] = decodeURIComponent($.getUrlParam('begin'));
    params['end'] = decodeURIComponent($.getUrlParam('end'));
    params['intervalbegin'] = decodeURIComponent($.getUrlParam('intervalbegin'));
    params['name'] = decodeURIComponent($.getUrlParam('name'));
    _(multiselectButtons).forEach(function(d) {
        params[d] = decodeURIComponent($.getUrlParam(d));
    });
    return params;  
};

function formatUrl(prefix, params) {
    return prefix + ($.isEmptyObject(params) ? '' : '?' + $.param(params));
};

function draw_detail_table() {
    var params = getParamsFromUrl();
    var geturl = formatUrl(Settings.apiUrl + '/fault_feature/feature_details/get' , params);

    $('#example').DataTable( {
        "dom": '<"top"CfB<"clear">>rt<"bottom"ip<"clear">>',
        paging: true,
        lengthMenu: [
                        [ 10, 100, -1 ],
                        [ '10 rows', '100 rows', 'Show all' ]
                    ],
        buttons: [
                    'pageLength', 'copy', 'csv', 'excel'
                 ],
        serverSide: false,
        ajax: {
            url: geturl,
            type: 'GET',
            dataSrc: 'results'
        },
        scrollX:false,
        autoWidth:false,
        columns: [
                    {title: formatTitleDate(params['interval']),
                     data: "createtimestamp", render: function(data, type, row) {
                        return formatDate(data*1000, params['interval']);
                    }},
                    {title: "Problem Report ID", data: "prid"},
                    {title: "Problem ID", data: "faultid"},
                    {title: "Title", data: "title"},
                    {title: "State", data: "state"},
                    {title: "SD Status", data: "debug" || ""},
                    {title: "Feature", data: "feature"},
                    {title: "TA Feature", data: "ta_feature"},
                    {title: "Top Importance", data: "top_importance"},
                    {title: "Days PR is open", data: "debug" || ""},
                    {title: "Days since PR GiC last Changed", data: "debug" || ""},

                    {title: "Duplicate", data: "debug" || ""},
                    {title: "PR Optional", data: "pr_optional"},
                    {title: "PR Additional", data: "pr_additional"},
                    {title: "Severity", data: "severity"},
                    {title: "Priority", data: "priority"},
                    {title: "Release", data: "release"},
                    {title: "SD Release", data: "debug" || ""},
                    {title: "SW Build", data: "sw_build"},
                    {title: "SD Build", data: "debug" || ""},
                    {title: "System Release", data: "debug" || ""},

                    {title: "Repeatability", data: "prrepeatability"},
                    {title: "PR Reported Date", data: "createtime"},
                    {title: "Author", data: "author"},
                    {title: "Author Group", data: "author_group"},
                    {title: "Discovered", data: "discovered"},
                    {title: "Test Subphase", data: "test_subphase"},
                    {title: "Group In Charge", data: "group_in_charge"},
                    {title: "Fault Coordinator", data: "development_fault_coordinator"},
                    {title: "BU", data: "debug" || ""},
                    {title: "Level4/DU", data: "debug" || ""},

                    {title: "Level4 Manager", data: "debug" || ""},
                    {title: "Plane Grouped", data: "debug" || ""},
                    {title: "System Component", data: "debug" || ""},
                    {title: "Platform", data: "platform"},
                    {title: "Shoule have been found in", data: "should_have_been_found_in"},
                    {title: "Root Cause", data: "root_cause"},
                    {title: "# of Transfers(Group)", data: "debug" || ""},
                    {title: "# of Transfers(SC)", data: "debug" || ""},
                    {title: "PROD", data: "prod"},
                    {title: "Product", data: "debug" || ""},

                    {title: "Product Line", data: "debug" || ""},
                    {title: "Type", data: "type"},
                    {title: "From Electra", data: "from_electra"},
                    {title: "Reported By", data: "reported_by"},
                    {title: "Customer", data: "customer"},
                    {title: "Close Enter", data: "closedenter", render: function(data, type, row) {
                        return data ? formatDate(data*1000, params['interval']) : "NA";
                    }},
                    {title: "Close Exit", data: "debug" || ""},
                    {title: "CF Creation Date", data: "debug" || ""},
                    {title: "CF Tested Date", data: "debug" || ""}

                 ],
        "columnDefs": [
//                             {data: null,
//                              defaultContent: "",
//                              targets: [6]},
//                              {data: null,
//                               defaultContent: "",
//                               targets: [23]}

                      ],
        order: [ 1, 'asc' ],
        "oLanguage": {
            "sLoadingRecords": "<img src='img/loading_3.gif'>  Loading "
        },
        bScrollInfinite: true

    } );

    function formatDate(timestamp, interval) {
        var formats = {day: 'YYYY-MM-DD', isoweek: 'YYYY[-]WW', month: 'YYYY[-]MM', year: 'YYYY', week: 'YYYY[-]ww'};
        return moment(timestamp).format(formats[interval]);
    };

    function formatTitleDate(interval) {
        var formats = {day: 'Year-Month-Day', isoweek: 'Year-Week', month: 'Year-Month', year: 'Year', week: 'Year-Week(Non ISO)'};
        return formats[interval];
    };
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

木瓜~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值