【问题标题】:Ignore first column in datatables CSV export忽略数据表 CSV 导出中的第一列
【发布时间】:2016-12-01 13:37:11
【问题描述】:

我正在尝试根据 jQuery 数据表中的数据导出 CSV 文件,我希望它排除第一列数据,这是我当前的脚本。

 <script>
 $(document).ready(function() {
 $('#example').DataTable( {
    dom: 'Bfrtip',
    lengthMenu: [
        [ 10, 25, 50, -1 ],
        [ '10 rows', '25 rows', '50 rows', 'Show all' ]
    ],
     buttons: [
        'copy', 'csv', 'excel', 'pdf', 'print', 'pageLength'
    ]
} );
} );
</script>

请问我需要添加什么才能使其正常工作

【问题讨论】:

    标签: jquery datatables


    【解决方案1】:

    您需要在buttons 定义中使用exportOptions

    buttons: [
           {
               extend: 'pdf',           
               exportOptions: {
                    columns: [1,2,3,4] // indexes of the columns that should be printed,
                }                      // Exclude indexes that you don't want to print.
           },
           {
               extend: 'csv',
               exportOptions: {
                    columns: [1,2,3,4] 
                }
    
           },
           {
               extend: 'excel',
               exportOptions: {
                    columns: [1,2,3,4] 
                }
           }         
        ]  
    

    【讨论】:

      【解决方案2】:

      接受的答案很好,但如果您不确定需要导出的列数,另一种方法将起作用:

      buttons: [
          {
              extend: 'excel', 
              exportOptions: {
                  columns: ':gt(0)' 
              }
          }
      ]
      

      第一列由偏移量 0 指定,因此此代码表示要导出任何大于 0 的列(即,除第一列之外的所有其他列)。

      【讨论】:

      • 我发现这是一个更好的答案,我认为它应该是公认的答案,因为除了第一个之外不需要明确说明列索引。假设您有一个包含 30 列的 DataTable。而不是columns: [ 1, 2, 3, ... 30],你可以使用这个快速的单行。此外,表结构可能会随着时间的推移而更新,并且可能会添加或删除列,因此此代码将独立于这些更改。
      猜你喜欢
      • 2011-03-15
      • 1970-01-01
      • 2016-02-17
      • 2012-07-06
      • 1970-01-01
      • 2011-10-09
      相关资源
      最近更新 更多