forked from snapappointments/bootstrap-select
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap-select.js
104 lines (82 loc) · 3.35 KB
/
bootstrap-select.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
!function($) {
var Selectpicker = function(element, options, e) {
if (e ) {
e.stopPropagation();
e.preventDefault();
}
this.$element = $(element);
this.$newElement = null;
this.selectClass = options.btnStyle || ''
this.init();
};
Selectpicker.prototype = {
contructor: Selectpicker,
init: function (e) {
this.$element.css('display', 'none');
var template = this.getTemplate();
template = this.createLi(template);
this.$element.after(template);
this.$newElement = this.$element.next('.bootstrap-select');
this.$newElement.find('> a').addClass(this.selectClass);
this.clickListener();
},
getTemplate: function() {
var template =
"<div class='btn-group bootstrap-select'>" +
"<a class='btn dropdown-toggle clearfix' data-toggle='dropdown' href='#''>" +
"<span class='filter-option pull-left'>__SELECTED_OPTION</span>" +
"<span class='caret pull-right'></span>" +
"</a>" +
"<ul class='dropdown-menu'>" +
"__ADD_LI" +
"</ul>" +
"</div>";
return template;
},
createLi: function(template) {
var _li = [];
var _liHtml = '';
var _this = this;
var _selected_index = this.$element.find('option:selected').index() ? this.$element.find('option:selected').index() : 0;
this.$element.find('option').each(function(){
_li.push($(this).text());
});
if(_li.length > 0) {
template = template.replace('__SELECTED_OPTION', _li[_selected_index]);
for (var i = 0; i < _li.length; i++) {
_liHtml += "<li rel=" + i + "><a href='#'>" + _li[i] + "</a></li>";
};
}
this.$element.find('option')[_selected_index].setAttribute('selected', 'selected');
template = template.replace('__ADD_LI', _liHtml);
return template;
},
clickListener: function() {
_this = this;
this.$newElement.find('li').on('click', function(e) {
e.preventDefault();
var rel = $(this).attr('rel');
$(this).parents('.bootstrap-select').prev('select')
.find('option').removeAttr('selected');
$(this).parents('.bootstrap-select').prev('select')
.find('option')[parseInt(rel,10)]
.setAttribute('selected', 'selected');
$(this).parents('.bootstrap-select')
.find('.filter-option').html($(this).text());
});
}
};
$.fn.selectpicker = function(option, event) {
return this.each(function () {
var $this = $(this),
data = $this.data('selectpicker'),
options = typeof option == 'object' && option;
if (!data) {
$this.data('selectpicker', (data = new Selectpicker(this, options, event)));
}
if (typeof option == 'string') {
data[option]();
}
});
};
}(window.jQuery);