gpt4 book ai didi

jquery - 如何更改导出时的列名称?

转载 作者:行者123 更新时间:2023-12-01 03:32:54 27 4
gpt4 key购买 nike

我想在导出时更改生成的 CSV 文件中的列名称。现在表和 CSV 文件显示“严重性”,我想在 CSV 中将名称更改为“严重”。

有什么办法可以做到这一点吗?

现在,我的代码如下所示:

opentable = $("#open").dataTable({
dom: "Bfrtip",
"data": openData,
"columns": [{
data: "severity", render: function (data, type, row) {
if (type === 'export') {
return data;
} else {
return '';
}
}
}],
"paging": true,
"searching": true,
buttons : [{
extend: 'csv',
exportOptions: {
columns: [0,1,2,3,4,5,6,7,8,9,10], orthogonal: 'export'
}
}]
});

最佳答案

根据csv option :

customize: Function that can be used to modify the contents of the exported data. The function takes two parameters, the data as configured by the button and the button's configuration object. The value that the function returns is the value that will be used for the export.

This can be particularly useful if you wish to add a company header or footer, description data or any other information to the exported data.

使用此选项,您可以将第一个导出行的列名称“Severity”更改为“Severe”:

customize: function (csv) {
var csvRows = csv.split('\n');
csvRows[0] = csvRows[0].replace('"Severity"', '"Severe"')
return csvRows.join('\n');
}

var openData = [
{
"name": "Tiger Nixon",
"position": "System Architect",
"salary": "$320,800",
"start_date": "2011\/04\/25",
"office": "Edinburgh",
"severity": "5421"
},
{
"name": "Donna Snider",
"position": "Customer Support",
"salary": "$112,000",
"start_date": "2011\/01\/25",
"office": "New York",
"severity": "4226"
}
];

var opentable = $("#open").dataTable({
dom: "Bfrtip",
"data": openData,
"columns": [
{data: 'name'},
{data: 'position'},
{data: 'office'},
{data: 'salary'},
{data: 'start_date'},
{
data: "severity",
render: function (data, type, row) {
if (type === 'export') {
return data;
} else {
return '';
}
}
}],
"paging": true,
"searching": true,
buttons: [{
extend: 'csv',
exportOptions: {
columns: [0, 1, 2, 3, 4, 5],
orthogonal: 'export'
},
filename: 'file',
fieldSeparator: ';',
customize: function (csv) {
var csvRows = csv.split('\n');
csvRows[0] = csvRows[0].replace('"Severity"', '"Severe"')
return csvRows.join('\n');
}
}]
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="//cdn.datatables.net/1.10.15/css/jquery.dataTables.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/buttons/1.3.1/css/buttons.dataTables.min.css">
<script src="//cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.3.1/js/dataTables.buttons.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.3.1/js/buttons.html5.min.js"></script>


<table id="open">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Salary</th>
<th>Start date</th>
<th>Severity</th>
</tr>
</thead>
</table>

关于jquery - 如何更改导出时的列名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44808592/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com