Hi, I’m using select2 and want to search list from start of string only.
This is a Wordpress site url: https://florapal.org/a-test-home-page/
this is my js:
I’m getting “Uncaught TypeError: text.toUpperCase is not a function”
and I only receive “Searching…” below input box - no results.
Any help much appreciated
I’m not familiar with select2, but a quick look at the documentation indicates that the two arguments passed into matcher are objects.
e.g.
matcher: function(params, data) {
// term and text are object properties
const term = params.term
const text = data.text
return text.toUpperCase().indexof(term.toUpperCase())==0;
}
Thanks for your help, my javascript skills are none-existent, so just copy-pasting, so far from the documentation (Custom Search) I have this:
jQuery(document).ready(function($) {
function matchCustom(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
if (data.text.indexOf(params.term) > -1) {
var modifiedData = $.extend({}, data, true);
modifiedData.text += ' (matched)';
// 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;
}
$(".js-select2").select2({
matcher: matchCustom
});
});
Which matches the input to anywhere in the string. How can I change that to search only from start of string, I also need to ignore case.