Skip to content

Commit ba24e83

Browse files
committed
Alternative texts for search using the option attribute data-alt
1 parent 1d93433 commit ba24e83

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

src/jquery.selectric.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@
472472
className : $elm.prop('class'),
473473
text : $elm.html(),
474474
slug : $.trim(_this.utils.replaceDiacritics($elm.html())),
475+
alt : $elm.attr('data-alt'),
475476
selected : $elm.prop('selected'),
476477
disabled : isDisabled
477478
};
@@ -627,10 +628,26 @@
627628
if ( val.length ) {
628629
// Search in select options
629630
$.each(_this.items, function(i, elm) {
630-
if ( !elm.disabled && searchRegExp.test(elm.text) || searchRegExp.test(elm.slug) ) {
631+
if (elm.disabled) {
632+
return;
633+
}
634+
if (searchRegExp.test(elm.text) || searchRegExp.test(elm.slug)) {
631635
_this.highlight(i);
632636
return;
633637
}
638+
if (!elm.alt) {
639+
return;
640+
}
641+
var altItems = elm.alt.split('|');
642+
for (var ai = 0; ai < altItems.length; ai++) {
643+
if (!altItems[ai]) {
644+
break;
645+
}
646+
if (searchRegExp.test(altItems[ai].trim())) {
647+
_this.highlight(i);
648+
return;
649+
}
650+
}
634651
});
635652
}
636653
});

test/basic.spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,24 @@ describe('basic suite', function() {
8080
expect(select.val()).toBe('banana');
8181
});
8282

83+
it('should search alternative text', function () {
84+
$('.selectric-input').val('alt banana').trigger('input');
85+
$('.selectric-items').find('.highlighted').click();
86+
expect(select.val()).toBe('banana');
87+
});
88+
89+
it('should search alternative text with separator', function () {
90+
$('.selectric-input').val('altberry').trigger('input');
91+
$('.selectric-items').find('.highlighted').click();
92+
expect(select.val()).toBe('bilberry');
93+
});
94+
95+
it('should search alternative text with separator 2', function () {
96+
$('.selectric-input').val('another berry').trigger('input');
97+
$('.selectric-items').find('.highlighted').click();
98+
expect(select.val()).toBe('bilberry');
99+
});
100+
83101
it('highlight() should return undefined if index is undefined', function () {
84102
expect(select.data('selectric').highlight(undefined)).toBe(undefined);
85103
});

test/fixtures/basic.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
<option value="loooooooooooooong-option">Loooooooooooooong option</option>
55
<option value="apple">Apple</option>
66
<option selected value="apricot">Apricot</option>
7-
<option class="customOptionClass" value="banana">Banana</option>
8-
<option value="bilberry">Bilberry</option>
7+
<option class="customOptionClass" value="banana"data-alt="alt banana">Banana</option>
8+
<option value="bilberry" data-alt="altberry | another berry">Bilberry</option>
99
<option value="blackberry">Blackberry</option>
1010
<option value="blackcurrant">Blackcurrant</option>
1111
<option value="blueberry">Blueberry</option>

0 commit comments

Comments
 (0)