-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnice-select.js
63 lines (51 loc) · 2.45 KB
/
nice-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
$(document).ready(function() {
// Generating custom select
$.fn.niceSelect = function(settings) {
let thisSelect = $(this);
$(this).css({
'display':'none',
});
let cssClass = settings.cssClass ? settings.cssClass : 'style-default';
let customSelectedText = settings.selectedText;
let customIcon = settings.customIcon ? settings.customIcon : '<i class="cs-arrow-down"></i>';
let thisOptions = thisSelect.find('option');
let niceSelect = $('<div class="custom-nice-select '+cssClass+'"><div class="selected closed"><span></span> '+customIcon+'</div><div class="options-list"></div></div>');
thisOptions.each(function() {
let optionText = $(this).html();
let optionValue = $(this).attr('value');
if ($(this).is(':selected')) {
let selectedText = customSelectedText ? customSelectedText : $(this).html();
niceSelect.find('.selected span').html(selectedText);
niceSelect.find('.options-list').append('<div data-value="'+optionValue+'" class="option option-selected">'+optionText+'</div>');
} else {
niceSelect.find('.options-list').append('<div data-value="'+optionValue+'" class="option">'+optionText+'</div>');
}
})
niceSelect.insertAfter(thisSelect);
}
// Triggering native select action
$(document).on('click','.custom-nice-select .option', function() {
let thisValue = $(this).data('value');
let thisNativeSelect = $(this).parent().parent().prev('select');
thisNativeSelect.val(thisValue).trigger('change');
})
// Toggling the dropdown on select click
$(document).on('click', '.custom-nice-select .selected', function() {
let thisOptions = $(this).next('.options-list');
if($(this).hasClass('closed')) {
thisOptions.slideDown('fast');
$(this).toggleClass('closed');
} else {
thisOptions.slideUp('fast');
$(this).toggleClass('closed');
}
})
// Closing the dropdown on outside click
$(document).on('click', function (e) {
if ($(e.target).closest('.custom-nice-select .selected').length === 0) {
$('.custom-nice-select .selected').addClass('closed');
let customSelects = $('.custom-nice-select .selected').next('.options-list');
customSelects.slideUp('fast');
}
});
})