Hi everyone !
I've been struggling with this for a couple of hours, and I'm not much of an expert in jQuery, or even Javascript.
Here's my situation: I've got a data array which looks like this:
- var availableTags = [
- {label: "xxx", common_name: "xxx", latin_name: "xxx", category: "xxx"},
- {label: "xxx", common_name: "xxx", latin_name: "xxx", category: "xxx"},
- {label: "xxx", common_name: "xxx", latin_name: "xxx", category: "xxx"},
- ...
- ]
After searching the API documentation related to "custom data and display", and browsing the web, I managed to implement a function allowing me to perform the comparison on all the fields in the array above.
Now, problem is, some fields may contain accented characters. Originally, I was working with an array, containing single values, like so:
- ["xxx", "xxx", "xxx", "xxx", ...]
So I just had to copy/paste the code from the "Accent folding" section from the Autocomplete widget, and it worked.
But, now, as you may have guessed, I need to combine those two functionalities. I need to compare the user input with the multiple fields, as shown above, while "ignoring" the accents.
Basically, I've got these two separated functions, which I used to assign to the "source":
For multiple fields search on custom data:- function lightwell(request, response) {
- function hasMatch(s) {
- if (s){
- return s.toLowerCase().indexOf(request.term.toLowerCase()) !== -1;
- }
- else
- return -1;
- }
- }
- var i, l, obj, matches = [];
- if (request.term === "") {
- response([]);
- return;
- }
- for (i = 0, l = availableTags.length; i<l; i++) {
- obj = availableTags[i];
- if (hasMatch(obj.label) || hasMatch(obj.common_name) || hasMatch(obj.latin_name) || hasMatch(obj.category)) {
- matches.push(obj);
- }
- }
- response(matches);
- }
For accent folding:- var accentMap = {"à":"a", "â":"a", "è":"e", "é":"e", etc. };
- var normalize = function(term) {
- var ret = "";
- for (var i = 0; i < term.length; i++) {
- ret += accentMap[term.charAt(i)] || term.charAt(i);
- }
- return ret;
- };
- function accentFolding(request, response){
- var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
- response($.grep(availableTags, function(value){
- value = value.label || value.value || value;
- return matcher.test(value) || matcher.test(normalize(value));
- }));
- }
Now I'm wondering how to combine these functions.
I assume the accent folding process should be included somewhere in my "lightwell" function, but I just can't figure out where. Besides, I believe I can't keep my "accentFolding" function as it is for use in "lightwell".
Any help will be much appreciated.
Thank you.