Skip to content

Instantly share code, notes, and snippets.

@jasper502
Last active March 13, 2016 04:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasper502/40b810e55b2195476342 to your computer and use it in GitHub Desktop.
Save jasper502/40b810e55b2195476342 to your computer and use it in GitHub Desktop.
select2 modification
function matcher (params, data) {
// Always return the object if there is nothing to compare
if ($.trim(params.term) === '') {
return data;
}
// MOVE THIS UP SO IF THE GROUP HITS A MATCH IT DOES NO SEARCH THE CHILDREN
var original = stripDiacritics(data.text).toUpperCase();
var oldterm = stripDiacritics(params.term).toUpperCase();
// Check if the text contains the term
if (original.indexOf(oldterm) > -1) {
return data;
}
// Do a recursive check for options with children
if (data.children && data.children.length > 0) {
// Clone the data object if there are children
// This is required as we modify the object to remove any non-matches
var match = $.extend(true, {}, data);
// Check each child of the option
for (var c = data.children.length - 1; c >= 0; c--) {
var child = data.children[c];
var matches = matcher(params, child);
// If there wasn't a match, remove the object in the array
if (matches == null) {
match.children.splice(c, 1);
}
}
// If any children matched, return the new object
if (match.children.length > 0) {
return match;
}
// If there were no matching children, check just the plain object
return matcher(params, match);
}
// If it doesn't contain the term, don't return anything
return null;
}
function matcher (params, data) {
// Always return the object if there is nothing to compare
if ($.trim(params.term) === '') {
return data;
}
// Do a recursive check for options with children
if (data.children && data.children.length > 0) {
// Clone the data object if there are children
// This is required as we modify the object to remove any non-matches
var match = $.extend(true, {}, data);
// Check each child of the option
for (var c = data.children.length - 1; c >= 0; c--) {
var child = data.children[c];
var matches = matcher(params, child);
// If there wasn't a match, remove the object in the array
if (matches == null) {
match.children.splice(c, 1);
}
}
// If any children matched, return the new object
if (match.children.length > 0) {
return match;
}
// If there were no matching children, check just the plain object
return matcher(params, match);
}
var original = stripDiacritics(data.text).toUpperCase();
var term = stripDiacritics(params.term).toUpperCase();
// Check if the text contains the term
if (original.indexOf(term) > -1) {
return data;
}
// If it doesn't contain the term, don't return anything
return null;
}
var matcher, stripDiacritics;
matcher = function(term, data) {
var c, child, match, matches, oldterm, original;
if ($.trim(term) === '') {
return data;
}
original = stripDiacritics(data.text).toUpperCase();
oldterm = stripDiacritics(term).toUpperCase();
if (original.indexOf(oldterm) > -1) {
return data;
}
if (data.children && data.children.length > 0) {
match = $.extend(true, {}, data);
c = data.children.length - 1;
while (c >= 0) {
child = data.children[c];
matches = matcher(params, child);
if (matches === null) {
match.children.splice(c, 1);
}
c--;
}
if (match.children.length > 0) {
return match;
}
return matcher(params, match);
}
return null;
};
stripDiacritics = function(text) {
var match;
match = function(a) {
return DIACRITICS[a] || a;
};
return text.replace(/[^\u0000-\u007E]/g, match);
};
$(document).ready(function() {
return $('[data-behavior="select2-asset-categories"]').each(function() {
return $(this).select2({
theme: 'bootstrap',
matcher: function(term, text) {
return matcher(term, text) !== null;
}
});
});
});
// ---
// generated by coffee-script 1.9.2
@jasper502
Copy link
Author

I think if I move the order the group options will stay open if the search term matches the group text. if not then it will search the group options.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment