matteodem / meteor-easy-search

Easy-to-use search for Meteor with Blaze Components
MIT License
437 stars 68 forks source link

Removing documents from search results #514

Closed MikeyParton closed 7 years ago

MikeyParton commented 8 years ago

Hi, I'm pretty new to Meteor, so sorry if this is a stupid question. Hopefully someone can point me in the right direction.

In my chat app, I have a page that uses an easysearch input to search the available users by username. To do this I've created an index of the profile in the Meteor.users collection. I want to know how I can remove the current user from the easy search results. So far I've done it with a template helper function - , but this isn't ideal because it just hides the result and it's pretty hacky. I would like to remove it to maintain correct functionality of .IfNoResults.

What is the best way to do this? A custom selector in the index? An outer function which removes the current user from the index?

Here's my index:

UsersIndex` = new EasySearch.Index({
    collection: Meteor.users,
    fields: ['profile.username'],
    engine: new EasySearch.Minimongo()
  });

Here's my helper:

Template.search.helpers({
    usersIndex: () => UsersIndex, 
    isCurrentUser:function(userId){
        if (Meteor.userId() == userId){
            return true;
            }    
    },
});

and my template:

<template name="search">
    {{> EasySearch.Input index=usersIndex}}<br>

    {{#EasySearch.IfNoResults index=usersIndex}}
      <div>No results found!</div>
    {{/EasySearch.IfNoResults}}

    {{#EasySearch.Each index=usersIndex }}
    {{#unless isCurrentUser _id}}    
        <div class="col-xs-6 col-sm-4 col-md-2">
            <div class="user_avatar">
                <a href="/chat/{{_id}}">
                    {{profile.username}}
                    <br/>
                    <img src="/{{profile.avatar}}" class="avatar_img">
                </a>
            </div>
        </div>
    {{/unless}}    
    {{/EasySearch.Each}}
matteodem commented 7 years ago

Please refer to the recipes mentioned here