您的位置:首页 > 运维架构

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

2017-03-08 16:30 661 查看
前端代码:

<!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];
};
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: