GPQAPP

Форк
0
235 строк · 6.0 Кб
1
/*!
2
 * Column visibility buttons for Buttons and DataTables.
3
 * 2016 SpryMedia Ltd - datatables.net/license
4
 */
5

6
(function( factory ){
7
	if ( typeof define === 'function' && define.amd ) {
8
		// AMD
9
		define( ['jquery', 'datatables.net', 'datatables.net-buttons'], function ( $ ) {
10
			return factory( $, window, document );
11
		} );
12
	}
13
	else if ( typeof exports === 'object' ) {
14
		// CommonJS
15
		module.exports = function (root, $) {
16
			if ( ! root ) {
17
				root = window;
18
			}
19

20
			if ( ! $ || ! $.fn.dataTable ) {
21
				$ = require('datatables.net')(root, $).$;
22
			}
23

24
			if ( ! $.fn.dataTable.Buttons ) {
25
				require('datatables.net-buttons')(root, $);
26
			}
27

28
			return factory( $, root, root.document );
29
		};
30
	}
31
	else {
32
		// Browser
33
		factory( jQuery, window, document );
34
	}
35
}(function( $, window, document, undefined ) {
36
'use strict';
37
var DataTable = $.fn.dataTable;
38

39

40
$.extend( DataTable.ext.buttons, {
41
	// A collection of column visibility buttons
42
	colvis: function ( dt, conf ) {
43
		var node = null;
44
		var buttonConf = {
45
			extend: 'collection',
46
			init: function ( dt, n ) {
47
				node = n;
48
			},
49
			text: function ( dt ) {
50
				return dt.i18n( 'buttons.colvis', 'Column visibility' );
51
			},
52
			className: 'buttons-colvis',
53
			closeButton: false,
54
			buttons: [ {
55
				extend: 'columnsToggle',
56
				columns: conf.columns,
57
				columnText: conf.columnText
58
			} ]
59
		};
60

61
		// Rebuild the collection with the new column structure if columns are reordered
62
		dt.on( 'column-reorder.dt'+conf.namespace, function (e, settings, details) {
63
			// console.log(node);
64
			// console.log('node', dt.button(null, node).node());
65
			dt.button(null, dt.button(null, node).node()).collectionRebuild([{
66
				extend: 'columnsToggle',
67
				columns: conf.columns,
68
				columnText: conf.columnText
69
			}]);
70
		});
71

72
		return buttonConf;
73
	},
74

75
	// Selected columns with individual buttons - toggle column visibility
76
	columnsToggle: function ( dt, conf ) {
77
		var columns = dt.columns( conf.columns ).indexes().map( function ( idx ) {
78
			return {
79
				extend: 'columnToggle',
80
				columns: idx,
81
				columnText: conf.columnText
82
			};
83
		} ).toArray();
84

85
		return columns;
86
	},
87

88
	// Single button to toggle column visibility
89
	columnToggle: function ( dt, conf ) {
90
		return {
91
			extend: 'columnVisibility',
92
			columns: conf.columns,
93
			columnText: conf.columnText
94
		};
95
	},
96

97
	// Selected columns with individual buttons - set column visibility
98
	columnsVisibility: function ( dt, conf ) {
99
		var columns = dt.columns( conf.columns ).indexes().map( function ( idx ) {
100
			return {
101
				extend: 'columnVisibility',
102
				columns: idx,
103
				visibility: conf.visibility,
104
				columnText: conf.columnText
105
			};
106
		} ).toArray();
107

108
		return columns;
109
	},
110

111
	// Single button to set column visibility
112
	columnVisibility: {
113
		columns: undefined, // column selector
114
		text: function ( dt, button, conf ) {
115
			return conf._columnText( dt, conf );
116
		},
117
		className: 'buttons-columnVisibility',
118
		action: function ( e, dt, button, conf ) {
119
			var col = dt.columns( conf.columns );
120
			var curr = col.visible();
121

122
			col.visible( conf.visibility !== undefined ?
123
				conf.visibility :
124
				! (curr.length ? curr[0] : false )
125
			);
126
		},
127
		init: function ( dt, button, conf ) {
128
			var that = this;
129
			button.attr( 'data-cv-idx', conf.columns );
130

131
			dt
132
				.on( 'column-visibility.dt'+conf.namespace, function (e, settings) {
133
					if ( ! settings.bDestroying && settings.nTable == dt.settings()[0].nTable ) {
134
						that.active( dt.column( conf.columns ).visible() );
135
					}
136
				} )
137
				.on( 'column-reorder.dt'+conf.namespace, function (e, settings, details) {
138
					// Button has been removed from the DOM
139
					if ( conf.destroying ) {
140
						return;
141
					}
142

143
					if ( dt.columns( conf.columns ).count() !== 1 ) {
144
						return;
145
					}
146

147
					// This button controls the same column index but the text for the column has
148
					// changed
149
					that.text( conf._columnText( dt, conf ) );
150

151
					// Since its a different column, we need to check its visibility
152
					that.active( dt.column( conf.columns ).visible() );
153
				} );
154

155
			this.active( dt.column( conf.columns ).visible() );
156
		},
157
		destroy: function ( dt, button, conf ) {
158
			dt
159
				.off( 'column-visibility.dt'+conf.namespace )
160
				.off( 'column-reorder.dt'+conf.namespace );
161
		},
162

163
		_columnText: function ( dt, conf ) {
164
			// Use DataTables' internal data structure until this is presented
165
			// is a public API. The other option is to use
166
			// `$( column(col).node() ).text()` but the node might not have been
167
			// populated when Buttons is constructed.
168
			var idx = dt.column( conf.columns ).index();
169
			var title = dt.settings()[0].aoColumns[ idx ].sTitle;
170

171
			if (! title) {
172
				title = dt.column(idx).header().innerHTML;
173
			}
174

175
			title = title
176
				.replace(/\n/g," ")        // remove new lines
177
				.replace(/<br\s*\/?>/gi, " ")  // replace line breaks with spaces
178
				.replace(/<select(.*?)<\/select>/g, "") // remove select tags, including options text
179
				.replace(/<!\-\-.*?\-\->/g, "") // strip HTML comments
180
				.replace(/<.*?>/g, "")   // strip HTML
181
				.replace(/^\s+|\s+$/g,""); // trim
182

183
			return conf.columnText ?
184
				conf.columnText( dt, idx, title ) :
185
				title;
186
		}
187
	},
188

189

190
	colvisRestore: {
191
		className: 'buttons-colvisRestore',
192

193
		text: function ( dt ) {
194
			return dt.i18n( 'buttons.colvisRestore', 'Restore visibility' );
195
		},
196

197
		init: function ( dt, button, conf ) {
198
			conf._visOriginal = dt.columns().indexes().map( function ( idx ) {
199
				return dt.column( idx ).visible();
200
			} ).toArray();
201
		},
202

203
		action: function ( e, dt, button, conf ) {
204
			dt.columns().every( function ( i ) {
205
				// Take into account that ColReorder might have disrupted our
206
				// indexes
207
				var idx = dt.colReorder && dt.colReorder.transpose ?
208
					dt.colReorder.transpose( i, 'toOriginal' ) :
209
					i;
210

211
				this.visible( conf._visOriginal[ idx ] );
212
			} );
213
		}
214
	},
215

216

217
	colvisGroup: {
218
		className: 'buttons-colvisGroup',
219

220
		action: function ( e, dt, button, conf ) {
221
			dt.columns( conf.show ).visible( true, false );
222
			dt.columns( conf.hide ).visible( false, false );
223

224
			dt.columns.adjust();
225
		},
226

227
		show: [],
228

229
		hide: []
230
	}
231
} );
232

233

234
return DataTable.Buttons;
235
}));
236

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.