-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.selectall.js
85 lines (76 loc) · 1.99 KB
/
jquery.selectall.js
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
81
82
83
84
85
(function($) {
checkedItems = [];
checkedAll = false;
var SelectAll = function(element, options) {
this.element = $(element);
this.settings = $.extend({
allSelector: ''
}, options || {});
$(this.element).on('click', $.proxy(this.click, this));
// If allSelector was provided, create listener for it
if(this.settings.allSelector != '') {
$(this.settings.allSelector).on('click', $.proxy(this.allClick, this));
}
this.append = function(id) {
var index = this.search(id);
if(index == -1)
checkedItems.push(id);
return checkedItems;
},
this.search = function(id) {
return checkedItems.indexOf(String(id));
},
this.removeID = function(id) {
var index = this.search(String(id));
if(index != -1)
checkedItems.splice(index, 1);
return checkedItems
}
this.removeIndex = function(index) {
checkedItems.splice(index, 1);
return checkedItems;
},
this.getList = function() {
return checkedItems;
}
};
SelectAll.prototype = {
// Click function for checkboxes
click: function() {
var id = $(this.element).val();
var index = this.search(id);
var checked = $(this.element).prop('checked')
if(checked) {
// Append
this.append(id);
} else {
this.removeIndex(index);
}
$(this.element).trigger('change',this.element);
},
// Click function for the select all button
allClick: function() {
var allSelector = $(this.settings.allSelector);
var checked = allSelector.prop('checked');
var id = $(this.element).val();
var index = this.search(id);
if(checked) {
if(index == -1) {
this.append(id);
}
$(this.element).prop('checked', true);
} else {
this.removeIndex(index);
$(this.element).prop('checked', false);
}
$(this.element).trigger('change',this.element);
}
};
$.fn.selectall = function(options) {
return this.each(function() {
var element = $(this);
var selectall = new SelectAll(this, options);
element.data('selectall', selectall);
});
};
})(jQuery);