I am tring to change how the search works, so that it searchs the first letters of the items. I am trying to use the matcher configuration as specified in the documentation, but just get no results found…can anyone see where I am going wrong?..thanks
<script type="text/javascript">
function matchStart(params, data) {
// If there are no search terms, return all of the data
if ($.trim(params.term) === ‘’) {
return data;
}
// Skip if there is no 'children' property
if (typeof data.children === 'undefined') {
return null;
}
// `data.children` contains the actual options that we are matching against
var filteredChildren = [];
$.each(data.children, function (idx, child) {
if (child.text.toUpperCase().indexOf(params.term.toUpperCase()) == 0) {
filteredChildren.push(child);
}
});
// If we matched any of the timezone group's children, then set the matched children on the group
// and return the group object
if (filteredChildren.length) {
var modifiedData = $.extend({}, data, true);
modifiedData.children = filteredChildren;
// You can return modified objects from here
// This includes matching the `children` how you want in nested data sets
return modifiedData;
}
// Return `null` if the term should not be displayed
return null;
}
$(function () {
$("#mySelect").select2({
matcher: matchStart
});
});
</script>
<select id="mySelect" name="mySelect">
<option value="0">Select</option>
<option value="1">Angelface</option>
<option value="2">Batcrazy</option>
<option value="3">Running Man</option>
<option value="4">The Scorpion King</option>
<option value="5">Cold Heart</option>
</select>