PK-1825
PK-1825

Reputation: 1489

How to hide column based on condition in Jquery Datatables?

I'm stuck at hiding a column in JQuery DataTables based on condition, I've radio button list based on checked value I need to display particular column in datatables how I can achieve this? I'm hiding column like below but when I click on Excel button all hidden column are displaying in Excel without any data

if ($scope.searchAtDefaultVal == 'Voucher') {
   // hide column for voucher
   $scope.voucherSearchTable.column(8).visible(false);
   $scope.voucherSearchTable.column(9).visible(false);
   $scope.voucherSearchTable.column(10).visible(false);
   $scope.voucherSearchTable.column(11).visible(true);
}
else {
   $scope.voucherSearchTable.column(8).visible(true);
   $scope.voucherSearchTable.column(9).visible(true);
   $scope.voucherSearchTable.column(10).visible(true);
   $scope.voucherSearchTable.column(11).visible(false);
}

This code is working, but the problem is when I click on Excel button hidden column also display in Excel sheet without data.

Upvotes: 0

Views: 803

Answers (1)

Chagai Wild
Chagai Wild

Reputation: 2163

When initializing the table, you can specify a selector of which data will be exported:

buttons: [
           {
             extend: 'excelHtml5',
             exportOptions: { 
                columns: ':visible' // <-- using datatables selector
            }
        }
    ]

That way only visible parts of the table will be exported, for more infomartion check https://datatables.net/extensions/buttons/examples/html5/columns.html

Upvotes: 1

Related Questions