admin管理员组文章数量:1401158
Is it possible to search on a bination of the text and another attribute in select2? For example, can the user search on both the value of "data-test" and text below:
<option value="185" data-test="5">Test</option>
<option value="186" data-test="6">Test 2</option>
<option value="187" data-test="7">Test 3</option>
Such that searching for "Test 2" and "6" show the same single option?
This is on a list bound at the page load, so it can't be filtered elsewhere.
Thanks -
Is it possible to search on a bination of the text and another attribute in select2? For example, can the user search on both the value of "data-test" and text below:
<option value="185" data-test="5">Test</option>
<option value="186" data-test="6">Test 2</option>
<option value="187" data-test="7">Test 3</option>
Such that searching for "Test 2" and "6" show the same single option?
This is on a list bound at the page load, so it can't be filtered elsewhere.
Thanks -
Share Improve this question asked Mar 15, 2016 at 20:53 James FJames F 5651 gold badge11 silver badges27 bronze badges4 Answers
Reset to default 4It's simple enough once found -
Create a custom matching function, similar to "matcher" in select2.js:
function customMatcher(params, data) {
// Always return the object if there is nothing to pare
if ($.trim(params.term) === '') {
return data;
}
... OTHER MATCHING CODE
// Check if the text contains the term
if (original.indexOf(term) > -1) {
return data;
}
// Check if the data occurs
if ($(data.element).data('test').toString().indexOf(params.term) > -1) {
return data;
}
// If it doesn't contain the term, don't return anything
return null;
}
I added the "data('test')" to the function above and updated the initialization to:
$("#ddl").select2({
matcher: customMatcher
});
Works great.
You can create your own custom matcher, here is your example:
function matchStart (term, text, option) {
console.log($(option.element).data("test"));
if (text.toUpperCase().indexOf(term.toUpperCase()) == 0 || ($(option.element).data("test") !== undefined && $(option.element).data("test") == term)) {
return true;
}
return false;
}
$.fn.select2.amd.require(['select2/pat/matcher'], function (oldMatcher) {
$("select").select2({
matcher: oldMatcher(matchStart)
})
});
The documentation about it you could find it here https://select2.github.io/announcements-4.0.html#new-matcher let me know if this suit your problem
I just added to select options data-country-name (since it was a select for country) which in oryginal had only short 'tags' of these countries (GB, AF, PL for example). Then in select2.js went to function matcher(params,data) which launch on every keyPress, and wrote:
var dataAttr = String(data.element.getAttribute('data-country-name'));
var termP = stripDiacritics(params.term).toUpperCase();
var org = stripDiacritics(dataAttr).toUpperCase();
if (org.indexOf(termP) > -1) {
return data;
}
You will have to work on the second selector i think but you can just do a simple if statement switching based off of if it was found the first way like so:
$( 'button' ).click(function() {
var found = $("option[data-test='6']");
if(typeof found == 'undefined'){
found = $('option[text="Test 2"]');
}
alert(found.val());
});
you will have to add a button too.
<button >Clicky</button>
本文标签: javascriptSearch on select attribute in select2Stack Overflow
版权声明:本文标题:javascript - Search on select attribute in select2 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744208749a2595316.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论