【问题标题】:Loading fields from collection into select field and filtering it based on selected value将集合中的字段加载到选择字段中并根据所选值对其进行过滤
【发布时间】:2015-08-21 23:18:56
【问题描述】:

基本上我有两个相关的问题,但我会用数字将它们分开 1)我正在尝试将单个字段加载到集合中的选择下拉框中,但它填充了它从下面的列表中收集的所有重复值,而不是它自己的助手。

<template name="carsList">

{{> categoryFilter}}

<ul class="collection" id="listings">
{{#each cars}}
<li>
  {{> carItem}}
</li>
{{/each}}
 </ul>
</template>

类别模板是

<template name="categoryFilter">
 <div class="input-field">
 <select>
   <option value="" disabled selected>Choose your option</option>
    {{#each companyCategories}}
     {{> companyCategory}}
    {{/each}}
 </select>
 <label>Select Category</label>
</div>
</template>

<template name="companyCategory">
  <option>{{ccategory}}</option>
</template>

categoryFilter 保存 iam 使用以下帮助程序的下拉模板

Template.categoryFilter.helpers({
companyCategories: function(){
return Cars.find();
}
});

而不是从它自己的助手中填充它加载来自它下面的“列表”的数据并重复数据。

Image of the result in selectbox

2)我还想根据选择下拉框中选择的值过滤列表(当然反应式)

请帮忙

编辑

这就是我的模板现在的样子

<template name="categoryFilter">
 <div class="input-field">
  <select>
   <option value="" disabled selected>Choose your option</option>
    {{#each companyCategories}}
      {{> companyCategory}}
    {{/each}}
   </select>
 <label>Select Category</label>
 </div>
</template>

<template name="companyCategory">
 <option>{{justCategory}}</option>
</template>

【问题讨论】:

    标签: javascript meteor meteor-helper


    【解决方案1】:

    这是我将如何进行的。首先,下拉列表:您应该使用光标而不是文档数组。所以你的助手应该是:

    Template.categoryFilter.helpers({
        companyCategories: function(){
           return Jobs.find();
        }
    });
    

    而您的categoryFilter 模板 HTML 可能是这样的

    <template name="categoryFilter">
    <div class="dropdown">
      <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-expanded="true">
        {{selectedCategory}} <!-- refer to an helper which returns the reactive variable--> 
        <span class="caret"></span>
      </button>
      <ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
      {{#each companyCategories}}
        <li role="presentation"><a role="menuitem" class="categoryItem" tabindex="-1" href="#">{{yourCatergoryNameField}}</a></li>
      {{/each}}
      </ul>
    </div>
    </template>
    

    然后您可以创建一个事件来记录单击时在下拉列表中选择的当前类别

        "click .categoryItem": function(e, t) {
            var text = $(e.target).text()
            Session.set("selectedCategory", text)
        },
    

    现在,您可以使用 selectedCategory 反应变量过滤您的汽车。为此,您需要另一个助手。例如:

    Template.carList.helpers({
        carListingByCategory: function(){
           var filter = Session.get ("selectedCategory");
           return Car.find(category:filter)
        }
    });
    

    这应该可以解决问题。我从不使用 Session,所以它可能无法像我想象的那样工作。如果它不起作用,请改用ReactiveDict。基本上,您在文档的开头添加var pageSession = new ReactiveDict();,您可以在我的代码中将Session 替换为pageSession

    编辑

    好的,让我们试试下划线。基于此answer,您可以执行以下操作来返回 allJobs 的每个项目的所有不同类别:

    Template.categoryFilter.helpers({
        companyCategories: _.uniq(Collection.find({}, {
            sort: {myField: 1}, fields: {myField: true}
        }).fetch().map(function(x) {
            return x.myField;
        }), true);
        }
    });
    

    myfield 是您的类别数组字段。

    编辑 2

    尝试更改您的html并将{{justCategory}}替换为{{this}}

    <template name="categoryFilter">
     <div class="input-field">
      <select>
       <option value="" disabled selected>Choose your option</option>
        {{#each companyCategories}}
          {{> companyCategory}}
        {{/each}}
       </select>
     <label>Select Category</label>
     </div>
    </template>
    
    <template name="companyCategory">
     <option>{{this}}</option>
    </template>
    

    【讨论】:

    • 还是一样的结果,请看我更新了我的代码并添加了一张图片来显示我得到的结果。它仍在加载从 carItem 模板获得的结果
    • 尝试在模板渲染函数中添加console.table(Jobs.find().fetch());。你也看到那里重复的项目吗?您的出版物可能有问题。仅供参考,如果两个出版物来自同一个集合,它们最终也会出现在同一个集合客户端。
    • 您应该在此页面中与多少 1) 收藏 2) 出版物进行交互?
    • 只有一个集合和两个出版物。我将为我的应用程序设置三个像这样的过滤器,它们将与同一个集合和一个为所有记录提供服务的出版物进行交互
    • 好的,那么您必须知道这两个发布最终将在同一个集合客户端。如果不了解您的收藏结构/出版物以及您正在寻找哪些领域(以及在哪里),我将无法提供更多帮助。您确定您的收藏在服务器上也没有重复的项目吗?您可以使用 robomongo 进行检查。
    猜你喜欢
    • 2015-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多