Meteor-Community-Packages / meteor-autocomplete

Client/server autocompletion designed for Meteor's collections and reactivity.
https://atmospherejs.com/mizzao/autocomplete
MIT License
350 stars 109 forks source link

Multiple field search & display 2 fields in textbox of same collection. #130

Open HemanthAnakapalle opened 8 years ago

HemanthAnakapalle commented 8 years ago

Hi,

I am looking for a use-case as below:

I am considering the same data as provided in the sample application.

Template.single.helpers({ settings: function() { return { position: Session.get("position"), limit: 10, rules: [ { // token: '', collection: StandardLegends, field: 'legend', matchAll: true, template: Template.standardLegends } ] }; }, legends: function() { return StandardLegends.find(); } });

In this case, it searches for a field 'legend' in the collection and displays the same in textbox in UI.

But I am looking for multiple fields search like 'legend', 'code', 'year' ... which mean ... even if I search with any of these values ... my search should succeed.

Along with this, it should also support displaying 2 fields in the textbox in UI like 'legend' & 'year'.

In my case, i would like to search on a users collection ... where user can search on any field like 'firstname', 'lastname', 'email' etc ... And since firstname and lastname are two different fields ... i would like display both in the textbox of UI.

Can you please help me providing the sample on your example .. so that i can follow the same on users collection ?

Advance Thanks.

HemanthAnakapalle commented 8 years ago

And another point to mention ..... I am not looking for any tokens here. It can be single/multiple rules ... that's not a problem.

jc2 commented 7 years ago

I dont know if this helps but i did the following to search in two fields (frist name and last name)

return {
          position: "bottom",
          limit: 10,
          rules: [{
              token: '',
              collection: Meteor.users,
              field: "_id",
              template: Template.userPill,
              selector: function(match){
                var terms = _.map(match.split(' '), function(key) {return new RegExp("^"+key, 'i'); });
                return {'$or': [{'profile.firstName': {'$in': terms}}, {'profile.lastName': {'$in': terms}}]};
              }
          }]
   };
<template name="userPill">
    <span class="">{{profile.firstName}} {{profile.lastName}}</span>
</template>
"autocompleteselect #userInfo": function(event, template, doc) {
    event.target.value = doc.profile.firstName + ' ' + doc.profile.lastName;
}
HemanthAnakapalle commented 7 years ago

Thanks for your time "Juan Camilo Ceron". I will try your suggestions and will update you.

Have a Great Day.

unknown4unnamed commented 7 years ago

@HemanthAnakapalle Here is my solution of the problem.