-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselectableTable.js
More file actions
80 lines (63 loc) · 1.93 KB
/
Copy pathselectableTable.js
File metadata and controls
80 lines (63 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* jQuery Selectable Table
* Author: Jan-Peter Krämer
* Copyright 2012 - Jan-Peter Krämer
*/
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */
/*global $ */
!function ($) {
var SelectableTable = function (element, options) {
this.element = $(element);
this.selectionClass = options.selectionClass;
var lastClickedRow;
var self = this;
this.element.on("click", "tbody tr", function (event) {
if (lastClickedRow === undefined) {
lastClickedRow = self.element.find('tbody tr:first');
if (lastClickedRow.length === 0) {
lastClickedRow = undefined;
} else {
lastClickedRow = lastClickedRow[0];
}
}
if (event.shiftKey) {
if ((lastClickedRow !== undefined) && (lastClickedRow !== this)) {
$(this).siblings().not(lastClickedRow).removeClass(self.selectionClass);
if ($(lastClickedRow).nextAll().is(this)) {
$(lastClickedRow).nextUntil(this).addClass(self.selectionClass);
} else {
$(this).nextUntil(lastClickedRow).addClass(self.selectionClass);
}
}
document.getSelection().removeAllRanges();
} else {
lastClickedRow = this;
}
$(this).toggleClass(self.selectionClass);
$(this).trigger("selectionChanged");
});
};
SelectableTable.prototype = {
constructor: SelectableTable,
getSelection: function () {
return this.element.find("tbody tr." + this.selectionClass);
}
};
$.fn.selectableTable = function (option, val) {
return this.each(function () {
var $this = $(this),
data = $this.data('selectableTable'),
options = (typeof option === "object") && option;
if (data === undefined) {
data = new SelectableTable(this, $.extend({}, $.fn.selectableTable.defaults, options));
$this.data('selectableTable', data);
}
if (typeof option === "string") {
data[option] = val;
}
});
};
$.fn.selectableTable.defaults = {
selectionClass: "selected"
};
}(window.jQuery)