UNPKG

5.55 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.bootstrapTableMultipleSelectionRow = 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.0
20 */
21
22 !function ($) {
23
24 'use strict';
25
26 document.onselectstart = function () {
27 return false;
28 };
29
30 var getTableObjectFromCurrentTarget = function getTableObjectFromCurrentTarget(currentTarget) {
31 currentTarget = $(currentTarget);
32 return currentTarget.is("table") ? currentTarget : currentTarget.parents().find(".table");
33 };
34
35 var getRow = function getRow(target) {
36 target = $(target);
37 return target.parent().parent();
38 };
39
40 var onRowClick = function onRowClick(e) {
41 var that = getTableObjectFromCurrentTarget(e.currentTarget);
42
43 if (window.event.ctrlKey) {
44 toggleRow(e.currentTarget, that, false, false);
45 }
46
47 if (window.event.button === 0) {
48 if (!window.event.ctrlKey && !window.event.shiftKey) {
49 clearAll(that);
50 toggleRow(e.currentTarget, that, false, false);
51 }
52
53 if (window.event.shiftKey) {
54 selectRowsBetweenIndexes([that.bootstrapTable("getOptions").multipleSelectRowLastSelectedRow.rowIndex, e.currentTarget.rowIndex], that);
55 }
56 }
57 };
58
59 var onCheckboxChange = function onCheckboxChange(e) {
60 var that = getTableObjectFromCurrentTarget(e.currentTarget);
61 clearAll(that);
62 toggleRow(getRow(e.currentTarget), that, false, false);
63 };
64
65 var toggleRow = function toggleRow(row, that, clearAll, useShift) {
66 if (clearAll) {
67 row = $(row);
68 that.bootstrapTable("getOptions").multipleSelectRowLastSelectedRow = undefined;
69 row.removeClass(that.bootstrapTable("getOptions").multipleSelectRowCssClass);
70 that.bootstrapTable("uncheck", row.data("index"));
71 } else {
72 that.bootstrapTable("getOptions").multipleSelectRowLastSelectedRow = row;
73 row = $(row);
74 if (useShift) {
75 row.addClass(that.bootstrapTable("getOptions").multipleSelectRowCssClass);
76 that.bootstrapTable("check", row.data("index"));
77 } else {
78 if (row.hasClass(that.bootstrapTable("getOptions").multipleSelectRowCssClass)) {
79 row.removeClass(that.bootstrapTable("getOptions").multipleSelectRowCssClass);
80 that.bootstrapTable("uncheck", row.data("index"));
81 } else {
82 row.addClass(that.bootstrapTable("getOptions").multipleSelectRowCssClass);
83 that.bootstrapTable("check", row.data("index"));
84 }
85 }
86 }
87 };
88
89 var selectRowsBetweenIndexes = function selectRowsBetweenIndexes(indexes, that) {
90 indexes.sort(function (a, b) {
91 return a - b;
92 });
93
94 for (var i = indexes[0]; i <= indexes[1]; i++) {
95 toggleRow(that.bootstrapTable("getOptions").multipleSelectRowRows[i - 1], that, false, true);
96 }
97 };
98
99 var clearAll = function clearAll(that) {
100 for (var i = 0; i < that.bootstrapTable("getOptions").multipleSelectRowRows.length; i++) {
101 toggleRow(that.bootstrapTable("getOptions").multipleSelectRowRows[i], that, true, false);
102 }
103 };
104
105 $.extend($.fn.bootstrapTable.defaults, {
106 multipleSelectRow: false,
107 multipleSelectRowCssClass: 'multiple-select-row-selected',
108 //internal variables used by the extension
109 multipleSelectRowLastSelectedRow: undefined,
110 multipleSelectRowRows: []
111 });
112
113 var BootstrapTable = $.fn.bootstrapTable.Constructor,
114 _init = BootstrapTable.prototype.init,
115 _initBody = BootstrapTable.prototype.initBody;
116
117 BootstrapTable.prototype.init = function () {
118 if (this.options.multipleSelectRow) {
119 var that = this;
120
121 //Make sure that the internal variables have the correct value
122 this.options.multipleSelectRowLastSelectedRow = undefined;
123 this.options.multipleSelectRowRows = [];
124
125 this.$el.on("post-body.bs.table", function (e) {
126 setTimeout(function () {
127 that.options.multipleSelectRowRows = that.$body.children();
128 that.options.multipleSelectRowRows.click(onRowClick);
129 that.options.multipleSelectRowRows.find("input[type=checkbox]").change(onCheckboxChange);
130 }, 1);
131 });
132 }
133
134 _init.apply(this, Array.prototype.slice.apply(arguments));
135 };
136
137 BootstrapTable.prototype.clearAllMultipleSelectionRow = function () {
138 clearAll(this);
139 };
140
141 $.fn.bootstrapTable.methods.push('clearAllMultipleSelectionRow');
142 }(jQuery);
143});
\No newline at end of file