【问题标题】:Meteor - how to get value of sort order from Mongo collection?Meteor - 如何从 Mongo 集合中获取排序顺序的值?
【发布时间】:2016-09-15 08:30:36
【问题描述】:

我正在尝试做的事情:

  • 通过遍历 Mongo 集合在页面上显示城市列表。
  • 在该列表旁边显示一个 Google 地图,并以城市的排序顺序 # 作为标记标签。

例如:

  1. 纽约 -> 在地图 div 中会有带有标签“1”的标记
  2. 芝加哥 -> 上面会有标签“2”的标记
  3. 华盛顿特区 -> 上面会有标签“3”的标记

简单的设置,我的 HTML 是:

<template name = "cities">
    {{#each city}}
        <p>{{cityname}}></p>
    {{/each}}
</template>

和JS:

Template.cities.helpers({
    'city': function(){
        var country = Session.get('currentCountry');
        return Cities.find({ country: country}, {sort: {population: -1}});
    }
});

一切都很好,但我想在我的城市旁边显示一个数字(这个数字是它们在 DOM 上加载的顺序,这取决于从检索城市列表时的排序方式蒙戈数据库)。

然后我需要以某种方式将此数字传递给 Google 地图标记创建功能。

感谢任何帮助!

【问题讨论】:

标签: mongodb google-maps meteor meteor-blaze


【解决方案1】:

事实证明这是微不足道的(感谢 Michel)

新的 HTML:

<template name = "cities">
    {{#each city}}
        <p>{{@index}} {{cityname}}></p>
    {{/each}}
</template>

如果您想将索引增加 1(或任何其他值),请参阅:Adding offset to {{@index}} when looping through items in Handlebars

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-23
    • 2014-08-30
    • 1970-01-01
    • 2018-06-27
    • 2014-05-18
    • 1970-01-01
    相关资源
    最近更新 更多