Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple keys: Return matches from both keys? #268

Closed
oleonard opened this issue Aug 11, 2021 · 7 comments
Closed

Multiple keys: Return matches from both keys? #268

oleonard opened this issue Aug 11, 2021 · 7 comments

Comments

@oleonard
Copy link

Sorry if I'm misunderstanding the documentation, but it looks to me from testing that if you specify multiple keys it will look for matches in one key or the other but not both. For example:

https://codepen.io/tarekraafat/pen/rNyBxdL

The data contains this:

    {
        "food": "Wild Boar - Tenderloin",
        "cities": "Luthu",
        "animals": "Eastern diamondback rattlesnake"
    },

A search for "wild" will return a result from that row, and a search for "luthu" will return a result from that row, but searching "wild luthu" will not return anything.

I'd like to be able to do a name search by firstname and surname so that a search for "owe leo" would return this row:

{
	"surname": "Leonard",
	"firstname": "Owen",
}

Is this currently possible?

@folknor
Copy link

folknor commented Aug 11, 2021

The only way to accomplish this at the moment is to use the searchEngine setting; https://tarekraafat.github.io/autoComplete.js/#/configuration?id=searchengine-optional

It's also best to avoid using data.keys then, otherwise your search function will be invoked once per key.

@oleonard
Copy link
Author

Thanks for the fast response! Are you referring do the searchEngine option of passing a function? Do you have any examples of how this works?

@folknor
Copy link

folknor commented Aug 12, 2021

new autoComplete({
	searchEngine: (q, r) => {
		if ( r.includes(q) ) {
			return r
		}
		return false
	}
})

If you don't use config.data.keys like I said, then r will be the entire record instead of the property at the keys, so

new autoComplete({
	searchEngine: (q, r) => {
		if ( r.surname.includes(q) ) {
			return r.surname
		}
		if ( r.firstname.includes(q) ) {
			return r.firstname
		}
		return false
	}
})

@devcer
Copy link

devcer commented Aug 28, 2021

Hi @folknor , I tried to use searchEngine but when I select an item, the selection is always undefined. any thoughts on it?

THis is my codepen - https://codepen.io/viswanathamsantosh/pen/wveBgob

@TarekRaafat
Copy link
Owner

TarekRaafat commented Aug 28, 2021

Hello @devcer,

The issue basically within the selection event, was not pointing to the proper values as shown below.

Before

autoCompleteJS.input.addEventListener("selection", function (event) {
  const feedback = event.detail;
  autoCompleteJS.input.blur();
  // Prepare User's Selected Value
  const selection = feedback.selection.value[feedback.selection.key];
  // Render selected choice to selection div
  document.querySelector(".selection").innerHTML = selection;
  // Replace Input value with the selected value
  autoCompleteJS.input.value = selection;
  // Console log autoComplete data feedback
  console.log(feedback);
});

After

autoCompleteJS.input.addEventListener("selection", function (event) {
  const feedback = event.detail;
  autoCompleteJS.input.blur();
  // Prepare User's Selected Value
  const name = feedback.selection.value.name;
  const email = feedback.selection.value.email;
  // Render selected choice to selection div
  document.querySelector(".selection").innerHTML = `${name} - ${email}`;
  // Replace Input value with the selected value
  autoCompleteJS.input.value = `${name} - ${email}`;
  // Console log autoComplete data feedback
  console.log(feedback);
});

Here is the fixed example.

I've enhanced your searchEngine algorithm as well as shown below.

Before

  searchEngine: (q, r) => {
    if (r.name.includes(q)) {
      return r;
    }
    if (r.email.includes(q)) {
      return r;
    }
    return false;
  },

After

  searchEngine: (q, r) => {
    const query = q.toLowerCase();
    const name = r.name.toLowerCase();
    const email = r.email.toLowerCase();

    if (name.includes(query)) {
      return r;
    }
    if (email.includes(query)) {
      return r;
    }
    return false;
  },

Let me know how it works.

Have a nice day! :)

@devcer
Copy link

devcer commented Aug 28, 2021

@TarekRaafat This works perfect! Thank you so much 😃

@TarekRaafat
Copy link
Owner

You're most welcome anytime! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants