UNPKG

4.25 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.bootstrapTableReorderRows = 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.0.1
20 */
21
22 (function ($) {
23
24 'use strict';
25
26 var isSearch = false;
27
28 var rowAttr = function rowAttr(row, index) {
29 return {
30 id: 'customId_' + index
31 };
32 };
33
34 $.extend($.fn.bootstrapTable.defaults, {
35 reorderableRows: false,
36 onDragStyle: null,
37 onDropStyle: null,
38 onDragClass: "reorder_rows_onDragClass",
39 dragHandle: null,
40 useRowAttrFunc: false,
41 onReorderRowsDrag: function onReorderRowsDrag(table, row) {
42 return false;
43 },
44 onReorderRowsDrop: function onReorderRowsDrop(table, row) {
45 return false;
46 },
47 onReorderRow: function onReorderRow(newData) {
48 return false;
49 }
50 });
51
52 $.extend($.fn.bootstrapTable.Constructor.EVENTS, {
53 'reorder-row.bs.table': 'onReorderRow'
54 });
55
56 var BootstrapTable = $.fn.bootstrapTable.Constructor,
57 _init = BootstrapTable.prototype.init,
58 _initSearch = BootstrapTable.prototype.initSearch;
59
60 BootstrapTable.prototype.init = function () {
61
62 if (!this.options.reorderableRows) {
63 _init.apply(this, Array.prototype.slice.apply(arguments));
64 return;
65 }
66
67 var that = this;
68 if (this.options.useRowAttrFunc) {
69 this.options.rowAttributes = rowAttr;
70 }
71
72 var onPostBody = this.options.onPostBody;
73 this.options.onPostBody = function () {
74 setTimeout(function () {
75 that.makeRowsReorderable();
76 onPostBody.apply();
77 }, 1);
78 };
79
80 _init.apply(this, Array.prototype.slice.apply(arguments));
81 };
82
83 BootstrapTable.prototype.initSearch = function () {
84 _initSearch.apply(this, Array.prototype.slice.apply(arguments));
85
86 if (!this.options.reorderableRows) {
87 return;
88 }
89
90 //Known issue after search if you reorder the rows the data is not display properly
91 //isSearch = true;
92 };
93
94 BootstrapTable.prototype.makeRowsReorderable = function () {
95 if (this.options.cardView) {
96 return;
97 }
98
99 var that = this;
100 this.$el.tableDnD({
101 onDragStyle: that.options.onDragStyle,
102 onDropStyle: that.options.onDropStyle,
103 onDragClass: that.options.onDragClass,
104 onDrop: that.onDrop,
105 onDragStart: that.options.onReorderRowsDrag,
106 dragHandle: that.options.dragHandle
107 });
108 };
109
110 BootstrapTable.prototype.onDrop = function (table, droppedRow) {
111 var tableBs = $(table),
112 tableBsData = tableBs.data('bootstrap.table'),
113 tableBsOptions = tableBs.data('bootstrap.table').options,
114 row = null,
115 newData = [];
116
117 for (var i = 0; i < table.tBodies[0].rows.length; i++) {
118 row = $(table.tBodies[0].rows[i]);
119 newData.push(tableBsOptions.data[row.data('index')]);
120 row.data('index', i).attr('data-index', i);
121 }
122
123 tableBsOptions.data = tableBsOptions.data.slice(0, tableBsData.pageFrom - 1).concat(newData).concat(tableBsOptions.data.slice(tableBsData.pageTo));
124
125 //Call the user defined function
126 tableBsOptions.onReorderRowsDrop.apply(table, [table, droppedRow]);
127
128 //Call the event reorder-row
129 tableBsData.trigger('reorder-row', newData);
130 };
131 })(jQuery);
132});
\No newline at end of file