Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从数据表导出excel文件时数据之间的间距_Javascript_Datatables - Fatal编程技术网

Javascript 从数据表导出excel文件时数据之间的间距

Javascript 从数据表导出excel文件时数据之间的间距,javascript,datatables,Javascript,Datatables,我在使用Datatables的导出文件上有这个bug。数据之间有一些不必要的空间 <script> $(document).ready(function () { $('.dataTablesWExport').DataTable({ dom: 'Bfrtip', buttons: [{ extend: 'excelHtml5', exportOptions

我在使用Datatables的导出文件上有这个bug。数据之间有一些不必要的空间

<script>
    $(document).ready(function () {
        $('.dataTablesWExport').DataTable({
            dom: 'Bfrtip',
            buttons: [{
              extend: 'excelHtml5',
              exportOptions: {
                columns: ':visible'
            }
        },
        {
          extend: 'pdfHtml5',
          exportOptions: {
            columns: ':visible'
        }
    },
    'colvis'
    ],
    "order": []
});
    });
</script>

$(文档).ready(函数(){
$('.dataTablesWExport').DataTable({
dom:'Bfrtip',
按钮:[{
扩展:“excelHtml5”,
出口选择:{
列:“:可见”
}
},
{
扩展:“pdfHtml5”,
出口选择:{
列:“:可见”
}
},
“科尔维斯”
],
“命令”:[]
});
});
导出文件的屏幕显示


您可以使用行功能来控制导出哪些行(包含数据的行)和哪些不导出(空行)

示例

假设我的表中有以下数据:

<table id="myTable" class="display nowrap dataTable cell-border" style="width:100%">
    <thead>
        <tr>
            <th>Name</th>
            <th>Position</th>
            <th>Office</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Adélaïde Fleury</td>
            <td>System Architect</td>
            <td>Paris</td>
        </tr>
        <tr>
            <td>John Smith</td>
            <td>Accountant</td>
            <td>London</td>
        </tr>
        <tr>
            <td></td>
            <td></td>
            <td>Tokyo</td>
        </tr>
    </tbody>
</table>
显示如下:

它使用
函数来测试每一行。如果行中的第一个单元格(
data[0]
)中包含数据,则该行将导出到Excel。但如果单元格为空、空或null,则不会导出该行:

它的工作原理

row函数为数据表中的每一行返回一个值
true
false
。因此,JavaScript将
数据[0]
中的非空值视为truthy,因此返回该行

因此,
返回数据[0]
只是一种更简洁的编写方法:

if ( data[0].length > 0 ) {
  return true;
} else {
  return false;
}
数据数组与数据对象的对比

我的源数据直接取自HTML表,因此它作为数据数组提供(每行一个数组):
['John Smith','accounter']
。这就是为什么我使用
data[0]
检查每行第一个单元格的内容

如果我的数据来自一个JSON对象,它可能看起来不同,例如:

{
  "name": "John Smith",
  "position": "Accountant"
},
{
  ...
}

在这种情况下,
数据[0]
将不起作用。我需要使用
data.name

您可以使用行函数来控制导出哪些行(包含数据的行)和哪些不导出(空白行)

示例

假设我的表中有以下数据:

<table id="myTable" class="display nowrap dataTable cell-border" style="width:100%">
    <thead>
        <tr>
            <th>Name</th>
            <th>Position</th>
            <th>Office</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Adélaïde Fleury</td>
            <td>System Architect</td>
            <td>Paris</td>
        </tr>
        <tr>
            <td>John Smith</td>
            <td>Accountant</td>
            <td>London</td>
        </tr>
        <tr>
            <td></td>
            <td></td>
            <td>Tokyo</td>
        </tr>
    </tbody>
</table>
显示如下:

它使用
函数来测试每一行。如果行中的第一个单元格(
data[0]
)中包含数据,则该行将导出到Excel。但如果单元格为空、空或null,则不会导出该行:

它的工作原理

row函数为数据表中的每一行返回一个值
true
false
。因此,JavaScript将
数据[0]
中的非空值视为truthy,因此返回该行

因此,
返回数据[0]
只是一种更简洁的编写方法:

if ( data[0].length > 0 ) {
  return true;
} else {
  return false;
}
数据数组与数据对象的对比

我的源数据直接取自HTML表,因此它作为数据数组提供(每行一个数组):
['John Smith','accounter']
。这就是为什么我使用
data[0]
检查每行第一个单元格的内容

如果我的数据来自一个JSON对象,它可能看起来不同,例如:

{
  "name": "John Smith",
  "position": "Accountant"
},
{
  ...
}

在这种情况下,
数据[0]
将不起作用。我需要使用
data.name

源数据是如何提供的,您检查了吗?您的问题是否可以显示HTML表中的一些示例数据?源数据是如何提供的,您检查了吗?您的问题是否可以显示HTML表中的一些示例数据?