【问题标题】:$firebaseArray move object from index to index$firebaseArray 将对象从索引移动到索引
【发布时间】:2015-05-13 01:47:00
【问题描述】:

在这里,我提取了一个交易列表,并将它们显示在一个启用了 show-reorder 的 ion-list 中

var ref = fb.child("/members/" + fbAuth.uid + "/accounts/" + $scope.AccountId + "/transactions/");
$scope.transactions = $firebaseArray(ref);

我有以下代码来处理排序

$scope.moveItem = function (transaction, fromIndex, toIndex) {
        $scope.transactions.splice(fromIndex, 1);
        $scope.transactions.splice(toIndex, 0, transaction);
};

我知道我在阅读docs here 时以错误的方式处理这个问题。但是,我无法找到对项目(在我的情况下为交易)进行排序并将这些更改保存回 Firebase 的方法。你能帮忙吗?

更新

根据下面的 Frank van Puffelen cmets,我正在添加其他信息。以以下交易为例。我从 Firebase 中提取以下交易,并按“customIndex”对其进行排序。因此,如果我在 customindex: "1" (麦当劳) 和 customindex: "2" (沃尔玛) 之间“移动” customindex: "5" (星巴克交易)

{
"accounts": {"Checking": 
    {payee: "McDonalds", amount: "2.35", customindex: "1"}
    {payee: "Walmart", amount: "78.12", customindex: "2"}
    {payee: "CapitalOne", amount: "150.00", customindex: "3"}
    {payee: "FootLocker", amount: "107.54", customindex: "4"}
    {payee: "Starbucks", amount: "2.88", customindex: "5"}
}}

我应该在 Firebase 中得到以下数据:

{
"accounts": {"Checking": 
    {payee: "McDonalds", amount: "2.35", customindex: "1"}
    {payee: "Starbucks", amount: "2.88", customindex: "2"}
    {payee: "Walmart", amount: "78.12", customindex: "3"}
    {payee: "CapitalOne", amount: "150.00", customindex: "4"}
    {payee: "FootLocker", amount: "107.54", customindex: "5"}
}}

我希望这有助于使其更清晰,并提前感谢您的帮助!

【问题讨论】:

  • 您的代码中的任何内容都没有任何排序。您想按什么顺序进行交易?

标签: firebase ionic-framework angularfire


【解决方案1】:

要订购您的物品,请将其交给 Firebase。例如,假设您希望在创建日期对交易进行排序,您可以:

var ref = fb.child("members")
            .child(fbAuth.uid)
            .child("accounts")
            .child($scope.AccountId)
            .child("transactions")
            .orderByChild("creationDate");
$scope.transactions = $firebaseArray(ref);

如果这未涵盖您想要的内容,请更新您的问题,以便更清楚地了解您想要排序的内容。目前,我不知道您要对什么进行排序。

更新

我刚刚注意到您的更新,这确实阐明了您要完成的工作。

在您的第一个数据 sn-p 中,数据按customindex 排序。因此,您可以按以下顺序检索它们:

var ref = fb.child("members")
            .child(fbAuth.uid)
            .child("accounts")
            .child($scope.AccountId)
            .child("transactions")
            .orderByChild("customindex");
$scope.transactions = $firebaseArray(ref);

在您的最后一个数据 sn-p 中,事务不再按任何属性排序。因此,当用户将 Starbucks 交易从最后一个位置拖放到第二个位置时,您需要更新其后所有交易的 customindex 属性。如果您更新这些值,AngularFire 将再次自动确保项目在$firebaseArray 中的顺序正确。

请注意,这需要更新 4 个事务,这可能效率不高。或者,您可以:

  1. 将交易详情(您最初拥有的)和交易顺序保存在不同的节点中。这样,您只需更新 transaction_order 节点。
  2. 在初始索引之间保留一些空间。所以不是 [1,2,3,4,5],而是从 [10,20,30,40,50] 开始。然后当用户在 20 到 30 之间移动 Starbucks 时,您可以简单地将 Starbucks 交易的customindex 更新为 25:[10,20,25,30,40,50]。这种方法有点老套,但实现起来更简单,通常效果很好。

【讨论】:

  • 这是我想用 ion-reorder-button Click Here 完成的示例,但我需要将更改保存在 Firebase 中
  • docs here 告诉我我不能使用 splice(),因为它不受 AngularFire 监控。但我不知道该用什么。
  • 我不知道您要订购的商品是什么。在 Firebase 中,项目集合不是按数组索引排序,而是按其他属性排序。默认为密钥,但可以通过任何orderBy 方法进行更改,例如我添加到我的 sn-p 中的方法。如果您的答案确实是“我想按索引排序”,那么将 index 属性添加到您的项目并进行操作。
  • 数字优先级可以是浮点数,所以它们之间不需要“留空间”。
猜你喜欢
  • 2023-04-05
  • 1970-01-01
  • 2021-12-15
  • 1970-01-01
  • 1970-01-01
  • 2018-07-31
  • 2015-05-18
  • 2017-09-30
  • 1970-01-01
相关资源
最近更新 更多