~~~
$('.deliveryAddress select').select2({
matcher: (params, data)=>{
// If there are no search terms, return all of the data
if ($.trim(params.term) === '') {
return data;
}
// Do not display the item if there is no 'text' property
if (typeof data.text === 'undefined') {
return null;
}
// `params.term` should be the term that is used for searching
// `data.text` is the text that is displayed for the data object
let keywords = params.term.split(' ');
let matchCounter = 0;
$.each(keywords, function(index, keyword) {
let text = data.text.toLowerCase();
text.indexOf(keyword.toLowerCase()) > -1 && matchCounter++;
});
if (matchCounter == keywords.length) {
// This includes matching the `children` how you want in nested data sets
return $.extend({}, data, true);
}
// Return `null` if the term should not be displayed
return null;
}
});
~~~