【问题标题】:Retrieving data from firebase while sorting it correctly从firebase检索数据,同时正确排序
【发布时间】:2017-02-03 23:35:39
【问题描述】:

我的 firebase 数据库中有这个结构

"metadata" : {
    "2017" : {
      "Februari" : {
        "-Kc3BF0V1iLgtnI65LuR" : {
          "date" : "2017-02-03T13:36:38.311Z",
          "price" : 90
        },
        "-Kc3BF0V1xxfrtnI65OlS" : {
          "date" : "2017-02-03T13:36:38.311Z",
          "price" : 90
        }
      },
      "Januari" : {
        "-Kc3E5bpxpo3RpSHH3fn" : {
          "date" : "2017-01-11T13:50:12.264Z",
          "price" : 45
        }
      }
    }
  }

我想要在我的应用中实现的是显示这样的列表

> Januari
>   - 45 (=price)
> 
> Februari
>   - 90 ()
>   - 240

问题是我可以显示月份,但由于 firebase 创建的唯一 ID,我无法联系到孩子。

这是我从 firebase 取回的对象

e{
    $$conf: Object
    $id: "2017"
    $priority: null
    Februari: Object
        -Kc3BF0V1iLgtnI65LuR: Object
        date: "2017-02-03T13:36:38.311Z"
        price: 90
    Januari: Object
        -Kc3E5bpxpo3RpSHH3fn: Object
        date: "2017-01-11T13:50:12.264Z"
        price: 45
}

我该如何处理?

【问题讨论】:

  • 如何在没有 firebase 创建的唯一键的情况下输入 childNode?这实际上是我的问题:)
  • 为什么不检索整个月份及其所有子项。将它们推送到一个数组并按以下方式对其子项进行排序: allChilds.sort((a, b) => a.price > b.price);

标签: angularjs firebase firebase-realtime-database angularfire


【解决方案1】:

您是否尝试使用 ng-repeat?像这样的东西对你有用吗:

<div ng-repeat="(name, month) in months">
    {{ name }}
    <div ng-repeat="entry in month">
        {{ entry.price }}
    </div>
</div>

几个月是database.ref().child('metaData').child('2017');的结果

【讨论】:

  • 这对我有用。但是你能解释一下{{entry.price}} 是如何工作的吗?我在哪里输入了具有由 firebase 创建的唯一键的对象,例如 -Kc3BF0V1iLgtnI65LuR
  • 它只是在一个对象上循环,你可以完成ng-repeat="(key, entry) in month",然后也拥有密钥
猜你喜欢
  • 2017-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-05
  • 2018-06-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多