Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Meteor中使用表单输入的简单搜索功能_Meteor_Spacebars - Fatal编程技术网

Meteor中使用表单输入的简单搜索功能

Meteor中使用表单输入的简单搜索功能,meteor,spacebars,Meteor,Spacebars,所以我正在尝试在我的Meteor应用程序中实现一个非常简单的搜索功能。这是一个非常简单的医学词典应用程序,因此我只有一个集合,其中包含所有术语及其各自的定义、发音等。我的目标是让用户使用表单输入输入他们的搜索查询,并显示相关的搜索结果(点击submit之后,在keyup或keydown事件之后,现在并不重要;这只是一个原型) 搜索栏(名为header.html的模板的一部分) 结果列表(在名为itemsList.html的模板中,这是不起作用的部分) {{{每本字典} {{/每个}}

所以我正在尝试在我的Meteor应用程序中实现一个非常简单的搜索功能。这是一个非常简单的医学词典应用程序,因此我只有一个集合,其中包含所有术语及其各自的定义、发音等。我的目标是让用户使用表单输入输入他们的搜索查询,并显示相关的搜索结果(点击submit之后,在keyup或keydown事件之后,现在并不重要;这只是一个原型)

搜索栏(名为
header.html的模板的一部分

结果列表(在名为
itemsList.html的模板中,这是不起作用的部分)


{{{每本字典}
{{/每个}}
在本例中,
{{english}
指的是我要搜索的集合中的数据块(集合
词典
中的英语单词)

所以,现在我已经解决了所有这些问题,我的问题是:从这里我该做什么?在
header.js
中,来自
console.log(Dictionary.find({english:query}.fetch());
的结果是/是对象我在寻找,所以基本上,我必须做什么才能将那个对象/那个些对象发送到我的
itemsList.html
模板,这样我就可以用光标在上面迭代


我认为我在这方面工作的时间太长了,因为我确信解决方案非常简单。如果我的方法完全错误,任何帮助都是值得感谢的(我见过很多人在搜索东西时使用会话),请让我知道我应该做得更好。另外,我正在使用iron router并已关闭autopublish,因此如果需要对其中任何一项进行更改,也请让我知道。谢谢!

只需将搜索字符串保存在会话中,并使用会话作为参数(如果存在)进行订阅,这样您就可以动态订阅添加到填充模板所需的数据

<form class="navbar-form navbar-left" role="search">
  <div class="form-group">
    <input type="text" id="search" name="search" class="form-control" placeholder="Search">
  </div>
  <button type="submit" class="btn btn-default">Submit</button>
</form>
Template.header.events({
'submit form': function(e) {
    e.preventDefault();
    var query = $(e.target).find('[name=search]').val();

    // Log the query for testing
    console.log(query);

    //Log the object that is returned for testing
    console.log(Dictionary.find({english: query}).fetch());

    var result = Dictionary.find({english: query}).fetch();

  }
});
<template name="itemsList">
  <div class="items">
    {{#each dictionary}}
      <ul>
        <!-- itemPage just refers to the page individual items -->
        <li><a href="{{pathFor 'itemPage'}}">{{english}}</a></li>
      </ul>
    {{/each}}
  </div>
</template>