UNPKG

7.01 kBJavaScriptView Raw
1(function (global, factory) {
2 if (typeof define === "function" && define.amd) {
3 define([], factory);
4 } else if (typeof exports !== "undefined") {
5 factory();
6 } else {
7 var mod = {
8 exports: {}
9 };
10 factory();
11 global.bootstrapTableReorderColumns = mod.exports;
12 }
13})(this, function () {
14 'use strict';
15
16 /**
17 * @author: Dennis Hernández
18 * @webSite: http://djhvscf.github.io/Blog
19 * @version: v1.1.0
20 */
21
22 !function ($) {
23
24 'use strict';
25
26 //From MDN site, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
27
28 var filterFn = function filterFn() {
29 if (!Array.prototype.filter) {
30 Array.prototype.filter = function (fun /*, thisArg*/) {
31 'use strict';
32
33 if (this === void 0 || this === null) {
34 throw new TypeError();
35 }
36
37 var t = Object(this);
38 var len = t.length >>> 0;
39 if (typeof fun !== 'function') {
40 throw new TypeError();
41 }
42
43 var res = [];
44 var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
45 for (var i = 0; i < len; i++) {
46 if (i in t) {
47 var val = t[i];
48
49 // NOTE: Technically this should Object.defineProperty at
50 // the next index, as push can be affected by
51 // properties on Object.prototype and Array.prototype.
52 // But that method's new, and collisions should be
53 // rare, so use the more-compatible alternative.
54 if (fun.call(thisArg, val, i, t)) {
55 res.push(val);
56 }
57 }
58 }
59
60 return res;
61 };
62 }
63 };
64
65 $.extend($.fn.bootstrapTable.defaults, {
66 reorderableColumns: false,
67 maxMovingRows: 10,
68 onReorderColumn: function onReorderColumn(headerFields) {
69 return false;
70 },
71 dragaccept: null
72 });
73
74 $.extend($.fn.bootstrapTable.Constructor.EVENTS, {
75 'reorder-column.bs.table': 'onReorderColumn'
76 });
77
78 var BootstrapTable = $.fn.bootstrapTable.Constructor,
79 _initHeader = BootstrapTable.prototype.initHeader,
80 _toggleColumn = BootstrapTable.prototype.toggleColumn,
81 _toggleView = BootstrapTable.prototype.toggleView,
82 _resetView = BootstrapTable.prototype.resetView;
83
84 BootstrapTable.prototype.initHeader = function () {
85 _initHeader.apply(this, Array.prototype.slice.apply(arguments));
86
87 if (!this.options.reorderableColumns) {
88 return;
89 }
90
91 this.makeRowsReorderable();
92 };
93
94 BootstrapTable.prototype.toggleColumn = function () {
95 _toggleColumn.apply(this, Array.prototype.slice.apply(arguments));
96
97 if (!this.options.reorderableColumns) {
98 return;
99 }
100
101 this.makeRowsReorderable();
102 };
103
104 BootstrapTable.prototype.toggleView = function () {
105 _toggleView.apply(this, Array.prototype.slice.apply(arguments));
106
107 if (!this.options.reorderableColumns) {
108 return;
109 }
110
111 if (this.options.cardView) {
112 return;
113 }
114
115 this.makeRowsReorderable();
116 };
117
118 BootstrapTable.prototype.resetView = function () {
119 _resetView.apply(this, Array.prototype.slice.apply(arguments));
120
121 if (!this.options.reorderableColumns) {
122 return;
123 }
124
125 this.makeRowsReorderable();
126 };
127
128 BootstrapTable.prototype.makeRowsReorderable = function () {
129 var that = this;
130 try {
131 $(this.$el).dragtable('destroy');
132 } catch (e) {}
133 $(this.$el).dragtable({
134 maxMovingRows: that.options.maxMovingRows,
135 dragaccept: that.options.dragaccept,
136 clickDelay: 200,
137 beforeStop: function beforeStop() {
138 var ths = [],
139 formatters = [],
140 columns = [],
141 columnsHidden = [],
142 columnIndex = -1,
143 optionsColumns = [];
144 that.$header.find('th').each(function (i) {
145 ths.push($(this).data('field'));
146 formatters.push($(this).data('formatter'));
147 });
148
149 //Exist columns not shown
150 if (ths.length < that.columns.length) {
151 columnsHidden = $.grep(that.columns, function (column) {
152 return !column.visible;
153 });
154 for (var i = 0; i < columnsHidden.length; i++) {
155 ths.push(columnsHidden[i].field);
156 formatters.push(columnsHidden[i].formatter);
157 }
158 }
159
160 for (var i = 0; i < this.length; i++) {
161 columnIndex = that.fieldsColumnsIndex[ths[i]];
162 if (columnIndex !== -1) {
163 that.columns[columnIndex].fieldIndex = i;
164 columns.push(that.columns[columnIndex]);
165 that.columns.splice(columnIndex, 1);
166 }
167 }
168
169 that.columns = that.columns.concat(columns);
170
171 filterFn(); //Support <IE9
172 $.each(that.columns, function (i, column) {
173 var found = false,
174 field = column.field;
175 that.options.columns[0].filter(function (item) {
176 if (!found && item["field"] == field) {
177 optionsColumns.push(item);
178 found = true;
179 return false;
180 } else return true;
181 });
182 });
183
184 that.options.columns[0] = optionsColumns;
185
186 that.header.fields = ths;
187 that.header.formatters = formatters;
188 that.initHeader();
189 that.initToolbar();
190 that.initBody();
191 that.resetView();
192 that.trigger('reorder-column', ths);
193 }
194 });
195 };
196 }(jQuery);
197});
\No newline at end of file