krisk / Fuse

Lightweight fuzzy-search, in JavaScript
https://fusejs.io/
Apache License 2.0
17.75k stars 753 forks source link

Is there a way to build a query on the fly? #462

Closed chrisjmccrum closed 3 years ago

chrisjmccrum commented 3 years ago

First off - want to say this library is great and very useful already! I'm looking to build a search panel that has more than just a simple text input for search. For the sake of simplicity, let's say I only have two search fields.

Text Input Keyword Search of Account Names

Dropdown Account Type [Options are: Any, Commercial, Residential, Government] (Default Value is Any)

The data object looks like this: [{ name: 'ABC Company', accountType: 'Commercial' }, { name: 'Lakehouse', accountType: 'Residential' }]

What I would like to do is to be able to either:


Perhaps I'm overcomplicating it and there's a much simpler approach lol? Any help would be appreciated
krisk commented 3 years ago

Can't you simply set a variable and modify it based on the search criteria? For example:

let conditions = [{ name: searchCriteria.keywords }]

if (searchCriteria.industry !== "any" {
  conditions.push({ accountType: searchCriteria.accountType )
}

var result = this.fuse.search({ $and: conditions })
chrisjmccrum commented 3 years ago

Thank you - I had a feeling I was over complicating it! This works just fine :)