【问题标题】:filter collection on dropdownlist change在下拉列表更改时过滤集合
【发布时间】:2014-12-29 01:58:49
【问题描述】:

我正在尝试在下拉列表更改时过滤我的收藏,这是我在模板管理器中的代码

  Template.postsList.events({
    "change .typeselection": function(e, t){
      console.log("drop changed");
      return Session.set("type", $("[name=type]").val());
    }
  });

这是我的模板代码:

  <template name="postsList">
   <select name="type" id="type"  value="" placeholder="type the adress" class="form-control typeselection">
            <option value="Restaurant">Restaurant</option>
             <option value="Hotel">Hotel</option>
             <option value="Shop">Shop</option>
        </select> 

  <div class="posts">
    {{#each postsWithRank}}
      {{> postItem}}
    {{/each}}

    {{#if nextPath}}
      <a class="load-more" href="{{nextPath}}">Load more</a>
    {{else}}
      {{#unless ready}}
        {{> spinner}}
      {{/unless}}
    {{/if}}
  </div>
</template>

【问题讨论】:

  • 我不知道如何过滤下拉列表更改的帖子。
  • 您能否将您的postsList 集合结构和postItem 模板代码添加到问题中,以便我们以更好的方式回答
  • 你能告诉我你通常如何过滤 dropdowlist 变化,而不关心我的文档结构吗?
  • 你确定它不应该是 Session.set('type',$(e.target).find('[name=type]').val() ?

标签: meteor


【解决方案1】:

据我了解,此代码适合您

//on dropdown change we are setting the new value in session
Template.postsList.events({
        "change .typeselection": function(e, t){
          console.log("drop changed");
          Session.set("type", $("[name=type]").val());//no need to return anything here
        }
      });




 //whenever the session variabe changes this code will re run
    Template.postsList.helpers({
        "postsWithRank": function(){
          //assuming you have type field in your collection and you want filter on that field
          return Posts.find({"type":Session.get("type")});
        }
      });



//so this code will display the selected relative data
 {{#each postsWithRank}}
  {{> postItem}}
{{/each}}

【讨论】:

    猜你喜欢
    • 2014-03-03
    • 1970-01-01
    • 2014-06-17
    • 1970-01-01
    • 1970-01-01
    • 2020-07-17
    • 1970-01-01
    • 1970-01-01
    • 2019-05-17
    相关资源
    最近更新 更多