-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathjQuery.select2Buttons.js
77 lines (72 loc) · 2.38 KB
/
jQuery.select2Buttons.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
/**
* .select2Buttons - Convert standard html select into button like elements
*
* Version: 1.0.1
* Updated: 2011-04-14
*
* Provides an alternative look and feel for HTML select buttons, inspired by threadless.com
*
* Author: Sam Cavenagh ([email protected])
* Doco and Source: https://github.com/o-sam-o/jquery.select2Buttons
*
* Licensed under the MIT
**/
jQuery.fn.select2Buttons = function(options) {
return this.each(function(){
var $ = jQuery;
var select = $(this);
var multiselect = select.attr('multiple');
select.hide();
var buttonsHtml = $('<div class="select2Buttons"></div>');
var selectIndex = 0;
var addOptGroup = function(optGroup){
if (optGroup.attr('label')){
buttonsHtml.append('<strong>' + optGroup.attr('label') + '</strong>');
}
var ulHtml = $('<ul class="select-buttons">');
optGroup.children('option').each(function(){
var liHtml = $('<li></li>');
if ($(this).attr('disabled') || select.attr('disabled')){
liHtml.addClass('disabled');
liHtml.append('<span>' + $(this).html() + '</span>');
}else{
liHtml.append('<a href="#" data-select-index="' + selectIndex + '">' + $(this).html() + '</a>');
}
// Mark current selection as "picked"
if((!options || !options.noDefault) && $(this).attr('selected')){
liHtml.children('a, span').addClass('picked');
}
ulHtml.append(liHtml);
selectIndex++;
});
buttonsHtml.append(ulHtml);
}
var optGroups = select.children('optgroup');
if (optGroups.length == 0) {
addOptGroup(select);
}else{
optGroups.each(function(){
addOptGroup($(this));
});
}
select.after(buttonsHtml);
buttonsHtml.find('a').click(function(e){
e.preventDefault();
var clickedOption = $(select.find('option')[$(this).attr('data-select-index')]);
if(multiselect){
if(clickedOption.attr('selected')){
$(this).removeClass('picked');
clickedOption.removeAttr('selected');
}else{
$(this).addClass('picked');
clickedOption.attr('selected', 'selected');
}
}else{
buttonsHtml.find('a, span').removeClass('picked');
$(this).addClass('picked');
clickedOption.attr('selected', 'selected');
}
select.trigger('change');
});
});
};