$firebaseArray将对象从索引移动到索引
在这里,我拉一个事务列表,并在启用了show reorder的离子列表中显示它们$firebaseArray将对象从索引移动到索引,firebase,ionic-framework,angularfire,Firebase,Ionic Framework,Angularfire,在这里,我拉一个事务列表,并在启用了show reorder的离子列表中显示它们 var ref = fb.child("/members/" + fbAuth.uid + "/accounts/" + $scope.AccountId + "/transactions/"); $scope.transactions = $firebaseArray(ref); 我有以下代码来处理排序 $scope.moveItem = function (transaction, fromIndex, to
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);
};
我知道我读了这本书之后就错了。但是,我无法找到对项目(在我的例子中是事务)进行排序并将这些更改保存回Firebase的方法。你能帮忙吗
更新
根据下面弗兰克·范·帕夫伦的评论,我添加了更多信息。以以下事务为例。我从Firebase中提取以下事务,并按“customIndex”排序。因此,如果我在customindex:“1”(麦当劳)和customindex:“2”(沃尔玛)之间“移动”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。例如,如果您希望在创建日期订购交易记录,您可以:
var ref = fb.child("members")
.child(fbAuth.uid)
.child("accounts")
.child($scope.AccountId)
.child("transactions")
.orderByChild("creationDate");
$scope.transactions = $firebaseArray(ref);
如果这还不包括你想要的,请更新你的问题,以便更清楚地了解你想要排序的内容。现在,我不知道你在想什么
更新
我刚刚注意到你的更新,这确实澄清了你想要实现的目标
在第一个数据片段中,数据按customindex
排序。因此,您可以通过以下方式按顺序检索它们:
var ref = fb.child("members")
.child(fbAuth.uid)
.child("accounts")
.child($scope.AccountId)
.child("transactions")
.orderByChild("customindex");
$scope.transactions = $firebaseArray(ref);
在上一个数据片段中,事务不再按任何属性排序。因此,当用户将Starbucks事务从最后一位拖到第二位时,您需要更新之后所有事务的customindex
属性。如果更新这些值,AngularFire将再次自动确保项目在$firebaseArray
中的顺序正确
请注意,这需要更新4个事务,这可能不是很有效。或者,您可以:
交易订单
节点customindex
更新为25:[10,20,25,30,40,50]。这种方法有点老套,但实现起来更简单,通常工作得很好代码中的任何内容都不会进行任何排序。您试图在什么上对事务进行排序?下面是一个示例,说明了我希望通过ion reorder按钮完成的操作,但我需要将更改保存在Firebase中。请告诉我无法使用splice(),因为AngularFire不监视它。但我不知道该用什么。我不知道的是你想订购什么。在Firebase中,项集合不是按数组索引排序的,而是按其他属性排序的。默认为键,但可以通过任何
orderBy
方法进行更改,就像我添加到代码段中的方法一样。如果您的答案真的是“我想按索引排序”,那么在您的项目中添加一个index
属性并对其进行操作。数字优先级可以是浮点数,因此不需要在它们之间“留出空间”。
var ref = fb.child("members")
.child(fbAuth.uid)
.child("accounts")
.child($scope.AccountId)
.child("transactions")
.orderByChild("customindex");
$scope.transactions = $firebaseArray(ref);