Skip to content

Commit cfccff3

Browse files
committed
New: autoFilter option for -button excelHtml5 which can be used to enable Excel's AutoFilter option on the table headers in the exported spreadsheet.
- https://datatables.net/forums/discussion/comment/130547
1 parent 13b1236 commit cfccff3

File tree

4 files changed

+87
-5
lines changed

4 files changed

+87
-5
lines changed

docs/button/excelHtml5.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,11 @@
234234
Message to be shown at the bottom of the table, or the `-tag caption` tag if displayed at the bottom of the table.
235235
</option>
236236

237+
<option type="boolean" name="autoFilter" default="false" since="1.5.4">
238+
Enable Excel's auto filter feature for the header cells in the table allowing the user to quickly filter and sort the exported spreadsheet in Excel. Note that this does not operate in LibreOffice (although the spreadsheet is still readable).
239+
</option>
240+
241+
237242

238243
<example title="DataTables initialisation: Use the HTML5 Excel button"><![CDATA[
239244
@@ -271,5 +276,18 @@ $('#myTable').DataTable( {
271276
]
272277
} );
273278
279+
]]></example>
280+
281+
<example title="Enable the auto filter option in Excel"><![CDATA[
282+
283+
$('#myTable').DataTable( {
284+
buttons: [
285+
{
286+
extend: 'excelHtml5',
287+
autoFilter: true
288+
}
289+
]
290+
} );
291+
274292
]]></example>
275293
</dt-button>

examples/html5/excelAutoFilter.xml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<dt-example table-type="html" order="8">
3+
4+
<css lib="datatables buttons"/>
5+
<js lib="jquery datatables buttons jszip buttons-html5">
6+
<![CDATA[
7+
8+
$(document).ready( function() {
9+
$('#example').DataTable( {
10+
dom: 'Bfrtip',
11+
buttons: [ {
12+
extend: 'excelHtml5',
13+
autoFilter: true,
14+
sheetName: 'Exported data'
15+
} ]
16+
} );
17+
} );
18+
19+
]]>
20+
</js>
21+
22+
<title lib="Buttons">Excel - auto filter</title>
23+
24+
<info><![CDATA[
25+
26+
Excel has an [AutoFilter](https://support.office.com/en-us/article/use-autofilter-to-filter-your-data-7d87d63e-ebd0-424b-8106-e2ab61133d92) feature which lets the end user quickly filter and sort data in the exported spreadsheet. To enable this feature on the header cells of the exported table, use the `autoFilter` option of the `-button excelHtml5` button type. Please note that the AutoFilter feature does no operate in LibreOffice, but the spreadsheet is still readable - this is a feature specific to Excel.
27+
28+
This example also shows the use of the `sheetName` option which allows the sheet in the exported workbook to be given a defined name.
29+
30+
]]></info>
31+
32+
</dt-example>

examples/html5/excelTextBold.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
2-
<dt-example table-type="html" order="8">
2+
<dt-example table-type="html" order="9">
33

44
<css lib="datatables buttons"/>
55
<js lib="jquery datatables buttons jszip buttons-html5">

js/buttons.html5.js

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -567,8 +567,9 @@ var excelStrings = {
567567
'<workbookView xWindow="0" yWindow="0" windowWidth="25600" windowHeight="19020" tabRatio="500"/>'+
568568
'</bookViews>'+
569569
'<sheets>'+
570-
'<sheet name="" sheetId="1" r:id="rId1"/>'+
570+
'<sheet name="Sheet1" sheetId="1" r:id="rId1"/>'+
571571
'</sheets>'+
572+
'<definedNames/>'+
572573
'</workbook>',
573574

574575
"xl/worksheets/sheet1.xml":
@@ -1009,6 +1010,7 @@ DataTable.ext.buttons.excelHtml5 = {
10091010

10101011
var that = this;
10111012
var rowPos = 0;
1013+
var dataStartRow, dataEndRow;
10121014
var getXml = function ( type ) {
10131015
var str = excelStrings[ type ];
10141016

@@ -1141,8 +1143,6 @@ DataTable.ext.buttons.excelHtml5 = {
11411143
rowPos++;
11421144
};
11431145

1144-
$( 'sheets sheet', xlsx.xl['workbook.xml'] ).attr( 'name', _sheetname( config ) );
1145-
11461146
if ( config.customizeData ) {
11471147
config.customizeData( data );
11481148
}
@@ -1171,15 +1171,20 @@ DataTable.ext.buttons.excelHtml5 = {
11711171
mergeCells( rowPos, data.header.length-1 );
11721172
}
11731173

1174+
11741175
// Table itself
11751176
if ( config.header ) {
11761177
addRow( data.header, rowPos );
11771178
$('row:last c', rels).attr( 's', '2' ); // bold
11781179
}
1180+
1181+
dataStartRow = rowPos;
11791182

11801183
for ( var n=0, ie=data.body.length ; n<ie ; n++ ) {
11811184
addRow( data.body[n], rowPos );
11821185
}
1186+
1187+
dataEndRow = rowPos;
11831188

11841189
if ( config.footer && data.footer ) {
11851190
addRow( data.footer, rowPos);
@@ -1207,6 +1212,29 @@ DataTable.ext.buttons.excelHtml5 = {
12071212
} ) );
12081213
}
12091214

1215+
// Auto filter for columns
1216+
$('mergeCells', rels).before( _createNode( rels, 'autoFilter', {
1217+
attr: {
1218+
ref: 'A'+dataStartRow+':'+createCellPos(data.header.length-1)+dataEndRow
1219+
}
1220+
} ) );
1221+
1222+
// Workbook modifications
1223+
var workbook = xlsx.xl['workbook.xml'];
1224+
1225+
$( 'sheets sheet', workbook ).attr( 'name', _sheetname( config ) );
1226+
1227+
if ( config.autoFilter ) {
1228+
$('definedNames', workbook).append( _createNode( workbook, 'definedName', {
1229+
attr: {
1230+
name: '_xlnm._FilterDatabase',
1231+
localSheetId: '0',
1232+
hidden: 1
1233+
},
1234+
text: _sheetname(config)+'!$A$'+dataStartRow+':'+createCellPos(data.header.length-1)+dataEndRow
1235+
} ) );
1236+
}
1237+
12101238
// Let the developer customise the document if they want to
12111239
if ( config.customize ) {
12121240
config.customize( xlsx, config, dt );
@@ -1261,7 +1289,11 @@ DataTable.ext.buttons.excelHtml5 = {
12611289

12621290
messageBottom: '*',
12631291

1264-
createEmptyCells: false
1292+
createEmptyCells: false,
1293+
1294+
autoFilter: false,
1295+
1296+
sheetName: ''
12651297
};
12661298

12671299
//

0 commit comments

Comments
 (0)