Skip to content

Commit f06ec1d

Browse files
authored
Merge pull request #78 from FabienLucini/patch-1
Update buttons.server-side.js
2 parents 3a4844d + 08cbfde commit f06ec1d

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

src/resources/assets/buttons.server-side.js

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
11
(function ($, DataTable) {
22
"use strict";
33

4-
var _buildParams = function (dt, action) {
4+
var _buildParams = function (dt, action, onlyVisibles) {
55
var params = dt.ajax.params();
66
params.action = action;
77
params._token = $('meta[name="csrf-token"]').attr('content');
88

9+
if (onlyVisibles) {
10+
params.visible_columns = _getVisibleColumns();
11+
} else {
12+
params.visible_columns = null;
13+
}
14+
915
return params;
1016
};
17+
18+
var _getVisibleColumns = function () {
19+
20+
var visible_columns = [];
21+
$.each(DataTable.settings[0].aoColumns, function (key, col) {
22+
if (col.bVisible) {
23+
visible_columns.push(col.name);
24+
}
25+
});
26+
27+
return visible_columns;
28+
};
1129

1230
var _downloadFromUrl = function (url, params) {
1331
var postUrl = url + '/export';
@@ -98,6 +116,21 @@
98116
_downloadFromUrl(url, params);
99117
}
100118
};
119+
120+
DataTable.ext.buttons.postExcelVisibleColumns = {
121+
className: 'buttons-excel',
122+
123+
text: function (dt) {
124+
return '<i class="fa fa-file-excel-o"></i> ' + dt.i18n('buttons.excel', 'Excel (only visible columns)');
125+
},
126+
127+
action: function (e, dt, button, config) {
128+
var url = dt.ajax.url() || window.location.href;
129+
var params = _buildParams(dt, 'excel', true);
130+
131+
_downloadFromUrl(url, params);
132+
}
133+
};
101134

102135
DataTable.ext.buttons.export = {
103136
extend: 'collection',
@@ -124,6 +157,21 @@
124157
}
125158
};
126159

160+
DataTable.ext.buttons.postCsvVisibleColumns = {
161+
className: 'buttons-csv',
162+
163+
text: function (dt) {
164+
return '<i class="fa fa-file-excel-o"></i> ' + dt.i18n('buttons.csv', 'CSV (only visible columns)');
165+
},
166+
167+
action: function (e, dt, button, config) {
168+
var url = dt.ajax.url() || window.location.href;
169+
var params = _buildParams(dt, 'csv', true);
170+
171+
_downloadFromUrl(url, params);
172+
}
173+
};
174+
127175
DataTable.ext.buttons.postCsv = {
128176
className: 'buttons-csv',
129177

0 commit comments

Comments
 (0)