Excluding last column of jQuery DataTables.net Tab

2020-03-31 07:36发布

Question: How can I exclude the last column of jQuery DataTables.net when using the TableTools extras to export?

Details

I'm initializing several different tables with jQuery DataTables.net and the TableTools extras. They all use the same initialization code.

The different tables have different number of columns. However, all tables have the last column in common, which is an 'action' column with buttons. When exporting with TableTools, it includes certain elements from this column in the export. I would like to exclude the action column for the TableTools exports for all tables.

I am aware of the mColumns option but it appears you need to know the number of columns, which doesn't work in my described scenario, so please don't give me answers such as this one:

"mColumns": [ 0, 1, 4 ]

Thanks

5条回答
不美不萌又怎样
2楼-- · 2020-03-31 07:50

In my case the best solution was to set the exportable attribute of the column to false. The same way you can set orderable, searchable, etc.

查看更多
Anthone
3楼-- · 2020-03-31 08:02

You can exclude using class names like this:

"aoColumnDefs": [{ "mColumns": false, "aTargets": ["no-export"] }],

Here is similar code I have working in production:

            var oTable = $('#<%= gvComputers.ClientID %>').dataTable({
            "bJQueryUI": true,
            "bStateSave": true,
            "sPaginationType": "full_numbers",
            "aLengthMenu": [[5, 10, 25, 50, 100, -1], [5, 10, 25, 50, 100, "All"]],
            "aoColumnDefs": [
                { "sSortDataType": "dom-text", "aTargets": ["text-sort_fixed"] },
                { "sType": "numeric", "aTargets": ["numeric-sort"] },
                { "sSortDataType": "dom-select", "aTargets": ["select-sort"] },
                { "sSortDataType": "dom-checkbox", "aTargets": ["checkbox-sort"] },
                { "bSearchable": false, "aTargets": ["no-search"] },
                { "bSortable": false, "aTargets": ["no-sort"] }
            ]
        });

I set the classes as needed on my table header, footer and cells dynamically since I do not know what columns the user will remove from their personal view of the data.

hth

查看更多
Explosion°爆炸
4楼-- · 2020-03-31 08:04

The best I've found so far is these hacks. Not particularly useful in my case, but maybe you have better luck

http://datatables.net/forums/discussion/327/tabletools-v1.0.2-save-as-excel-csv-copy-and-print/p3

查看更多
孤傲高冷的网名
5楼-- · 2020-03-31 08:12

Suppose you have six columns, and you only want to display columns 1, 2, 3 and 5. Try this :

{
   text: "print",
   extend: "print",
   className: "btn btn-lg btn-danger",
   exportOptions:
     {
       columns: [0,1,2,4]}
     }
}
查看更多
神经病院院长
6楼-- · 2020-03-31 08:14

Just for googlers: in tabletools 2.2.3 you can now use function for mColumns

var dataTable = $grdData.DataTable({
     tableTools: {
          aButtons: [{
              "sExtends": "csv",
              "sButtonText": "csv",
              "mColumns": function ( dtSettings ) {
                   var api = new $.fn.dataTable.Api( dtSettings );

                   return api.columns(":not(:last)").indexes().toArray();
              }
          }]
     }
});
查看更多
登录 后发表回答