导出带有按钮集合的选定行在dataTables中不起作用 [英] Export selected rows with buttons collection not working in dataTables

查看:66
本文介绍了导出带有按钮集合的选定行在dataTables中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图仅从dataTable中导出选定的行。我可以通过将扩展选项设置为csv来成功地将选定的行导出到csv文件,如下面的代码所示。

I am trying to export only the selected rows from a dataTable. I could successfully export selected rows to a csv file by setting extend option to csv as shown in the below code.

buttons: [
    'colvis',
    'selectAll',
    'selectNone',
    {
        extend: 'csv',
        text: 'Export Selected',
        exportOptions: {
            columns: ':visible:not(.not-exported)',
            modifier: {
                 selected: true
            }
        },
        title: 'Data export'
    }
],

但是我想要一个下拉菜单(csv,复印,打印),可以从中选择导出选择的行。我尝试使用以下代码中的集合。但是它导出所有可见行。有人请帮助

But I want to have a drop down(csv, copy, print) from which I can choose to export the selcted rows to. I tried using collection as in the below code. But it exports all the visible rows. Someone pls help

buttons: [
    'colvis',
    'selectAll',
    'selectNone',
    {
        extend: 'collection',
        text: 'Export Selected',
        buttons: ['copy','csv','print'],
        exportOptions: {
            columns: ':visible:not(.not-exported)',
            modifier: {
                 selected: true
            }
        },
        title: 'Data export'
    }
],


推荐答案

您只需使用行:'.selected'来定义仅选定。但是:即使使用集合,您仍将需要为每个按钮提供设置。即

You define "selected only" simply by using rows: '.selected'. However: Even though you are using a collection, you will still need to provide settings for each button. I.e

buttons: ['copy','csv','print'],

应该是

buttons: [
  { extend :'copy',
    exportOptions : {
     columns: ':visible:not(.not-exported)',
     rows: '.selected'
  }
  ...
]

您可以通过重新使用简单的文字来减少代码量

You can reduce the amount of code by reusing a simple literal

var exportOptions = {
  columns: ':visible:not(.not-exported)',
  rows: '.selected'
}

工作示例代码将最终如下所示:

The working sample code will end up like this :

buttons: [
  'colvis',
  'selectAll',
  'selectNone',
  {
    extend: 'collection',
    text: 'Export Selected',
    buttons: [
      { extend : 'copy',
        exportOptions: exportOptions
      },
      { extend : 'csv',
        exportOptions: exportOptions
      },
      { extend : 'print',
        exportOptions: exportOptions
      }
    ]   
  }
]

这是一个演示-> https://jsfiddle.net/youn7zm4/

here is a demo -> https://jsfiddle.net/youn7zm4/

这篇关于导出带有按钮集合的选定行在dataTables中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆