如何从 Meteor Collection 中搜索和输出 [英] How to Search and output from Meteor Collection

查看:46
本文介绍了如何从 Meteor Collection 中搜索和输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JS

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

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

所以这段代码会将Col.find中的所有内容输出到html中的handlebar cols中

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

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.

示例:输入 = "bl"输出应该是黑色、蓝色或其他以 bl 开头的内容.

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());
  }
});

推荐答案

您需要以保留 Meteor 反应性的方式使模板的内容依赖于输入字段.会话变量对此非常有用.

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.

这应该有效:

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());
  }
});

这篇关于如何从 Meteor Collection 中搜索和输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆