| 9 |
lars |
1 |
/*!
|
|
|
2 |
* Column visibility buttons for Buttons and DataTables.
|
|
|
3 |
* 2015 SpryMedia Ltd - datatables.net/license
|
|
|
4 |
*/
|
|
|
5 |
|
|
|
6 |
(function($, DataTable) {
|
|
|
7 |
"use strict";
|
|
|
8 |
|
|
|
9 |
|
|
|
10 |
$.extend( DataTable.ext.buttons, {
|
|
|
11 |
// A collection of column visibility buttons
|
|
|
12 |
colvis: function ( dt, conf ) {
|
|
|
13 |
return {
|
|
|
14 |
extend: 'collection',
|
|
|
15 |
text: function ( dt ) {
|
|
|
16 |
return dt.i18n( 'buttons.colvis', 'Column visibility' );
|
|
|
17 |
},
|
|
|
18 |
className: 'buttons-colvis',
|
|
|
19 |
buttons: [ {
|
|
|
20 |
extend: 'columnsToggle',
|
|
|
21 |
columns: conf.columns
|
|
|
22 |
} ]
|
|
|
23 |
};
|
|
|
24 |
},
|
|
|
25 |
|
|
|
26 |
// Selected columns with individual buttons - toggle column visibility
|
|
|
27 |
columnsToggle: function ( dt, conf ) {
|
|
|
28 |
var columns = dt.columns( conf.columns ).indexes().map( function ( idx ) {
|
|
|
29 |
return {
|
|
|
30 |
extend: 'columnToggle',
|
|
|
31 |
columns: idx
|
|
|
32 |
};
|
|
|
33 |
} ).toArray();
|
|
|
34 |
|
|
|
35 |
return columns;
|
|
|
36 |
},
|
|
|
37 |
|
|
|
38 |
// Single button to toggle column visibility
|
|
|
39 |
columnToggle: function ( dt, conf ) {
|
|
|
40 |
return {
|
|
|
41 |
extend: 'columnVisibility',
|
|
|
42 |
columns: conf.columns
|
|
|
43 |
};
|
|
|
44 |
},
|
|
|
45 |
|
|
|
46 |
// Selected columns with individual buttons - set column visibility
|
|
|
47 |
columnsVisibility: function ( dt, conf ) {
|
|
|
48 |
var columns = dt.columns( conf.columns ).indexes().map( function ( idx ) {
|
|
|
49 |
return {
|
|
|
50 |
extend: 'columnVisibility',
|
|
|
51 |
columns: idx,
|
|
|
52 |
visibility: conf.visibility
|
|
|
53 |
};
|
|
|
54 |
} ).toArray();
|
|
|
55 |
|
|
|
56 |
return columns;
|
|
|
57 |
},
|
|
|
58 |
|
|
|
59 |
// Single button to set column visibility
|
|
|
60 |
columnVisibility: {
|
|
|
61 |
columns: null, // column selector
|
|
|
62 |
text: function ( dt, button, conf ) {
|
|
|
63 |
return conf._columnText( dt, conf.columns );
|
|
|
64 |
},
|
|
|
65 |
className: 'buttons-columnVisibility',
|
|
|
66 |
action: function ( e, dt, button, conf ) {
|
|
|
67 |
var col = dt.column( conf.columns );
|
|
|
68 |
|
|
|
69 |
col.visible( conf.visibility !== undefined ?
|
|
|
70 |
conf.visibility :
|
|
|
71 |
! col.visible()
|
|
|
72 |
);
|
|
|
73 |
},
|
|
|
74 |
init: function ( dt, button, conf ) {
|
|
|
75 |
var that = this;
|
|
|
76 |
var col = dt.column( conf.columns );
|
|
|
77 |
|
|
|
78 |
dt
|
|
|
79 |
.on( 'column-visibility.dt'+conf.namespace, function (e, settings, column, state) {
|
|
|
80 |
if ( column === conf.columns ) {
|
|
|
81 |
that.active( state );
|
|
|
82 |
}
|
|
|
83 |
} )
|
|
|
84 |
.on( 'column-reorder.dt'+conf.namespace, function (e, settings, details) {
|
|
|
85 |
var col = dt.column( conf.columns );
|
|
|
86 |
|
|
|
87 |
button.text( conf._columnText( dt, conf.columns ) );
|
|
|
88 |
that.active( col.visible() );
|
|
|
89 |
} );
|
|
|
90 |
|
|
|
91 |
this.active( col.visible() );
|
|
|
92 |
},
|
|
|
93 |
destroy: function ( dt, button, conf ) {
|
|
|
94 |
dt
|
|
|
95 |
.off( 'column-visibility.dt'+conf.namespace )
|
|
|
96 |
.off( 'column-reorder.dt'+conf.namespace );
|
|
|
97 |
},
|
|
|
98 |
|
|
|
99 |
_columnText: function ( dt, col ) {
|
|
|
100 |
// Use DataTables' internal data structure until this is presented
|
|
|
101 |
// is a public API. The other option is to use
|
|
|
102 |
// `$( column(col).node() ).text()` but the node might not have been
|
|
|
103 |
// populated when Buttons is constructed.
|
|
|
104 |
var idx = dt.column( col ).index();
|
|
|
105 |
return dt.settings()[0].aoColumns[ idx ].sTitle
|
|
|
106 |
.replace(/\n/g," ") // remove new lines
|
|
|
107 |
.replace( /<.*?>/g, "" ) // strip HTML
|
|
|
108 |
.replace(/^\s+|\s+$/g,""); // trim
|
|
|
109 |
}
|
|
|
110 |
},
|
|
|
111 |
|
|
|
112 |
|
|
|
113 |
colvisRestore: {
|
|
|
114 |
className: 'buttons-colvisRestore',
|
|
|
115 |
|
|
|
116 |
text: function ( dt ) {
|
|
|
117 |
return dt.i18n( 'buttons.colvisRestore', 'Restore visibility' );
|
|
|
118 |
},
|
|
|
119 |
|
|
|
120 |
init: function ( dt, button, conf ) {
|
|
|
121 |
conf._visOriginal = dt.columns().indexes().map( function ( idx ) {
|
|
|
122 |
return dt.column( idx ).visible();
|
|
|
123 |
} ).toArray();
|
|
|
124 |
},
|
|
|
125 |
|
|
|
126 |
action: function ( e, dt, button, conf ) {
|
|
|
127 |
dt.columns().every( function ( i ) {
|
|
|
128 |
this.visible( conf._visOriginal[ i ] );
|
|
|
129 |
} );
|
|
|
130 |
}
|
|
|
131 |
},
|
|
|
132 |
|
|
|
133 |
|
|
|
134 |
colvisGroup: {
|
|
|
135 |
className: 'buttons-colvisGroup',
|
|
|
136 |
|
|
|
137 |
action: function ( e, dt, button, conf ) {
|
|
|
138 |
dt.columns( conf.show ).visible( true );
|
|
|
139 |
dt.columns( conf.hide ).visible( false );
|
|
|
140 |
},
|
|
|
141 |
|
|
|
142 |
show: [],
|
|
|
143 |
|
|
|
144 |
hide: []
|
|
|
145 |
}
|
|
|
146 |
} );
|
|
|
147 |
|
|
|
148 |
|
|
|
149 |
})(jQuery, jQuery.fn.dataTable);
|