How to Search and output from Meteor Collection

安稳与你 提交于 2019-12-25 03:12:23

问题


The JS

Colors = new Meteor.Collection("colors");

Template.col_list.cols = function () {
  return Colors.find();
};

So this code will output everything from Col.find into the handlebar cols in the html

The HTML

{{#each cols}}
{{name}}, {{RGB}}, {{HEX}}
{{/each}}

But say I want to have a input box so I can search for a name of the color, so it only shows the information for that color or any color that starts with whatever I type into the input box.

example: Input = "bl" output should be black, blue or whatever else that starts with bl.

I've been looking everywhere for any example of this in practice yet I haven't found any.

Far as I have gotten so far is something to this effect yet it yields no luck.

Template.col_list.events({
  'click .search': function() {
    Colors.find(name: $('col_name').val());
  }
});

回答1:


You need to make the content of the template depend on the input field in a way that preserves the reactivity of Meteor. Session variables are great for that.

This should work:

Colors = new Meteor.Collection("colors");

Template.col_list.cols = function () {
  return Colors.find({name: { $regex: Session.get('prefix')+".*", $options: 'i' }});
};

Template.col_list.events({
  'click .search': function() {
    Session.set('prefix', $('.col_name').val());
  }
});


来源:https://stackoverflow.com/questions/20664758/how-to-search-and-output-from-meteor-collection

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!