在这里,我提取了一个事务列表,并在启用了show-reorder的离子列表中显示它们
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的评论,我正在添加其他信息。以下面的交易为例。我从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"}
}}
我希望这有助于使其更加清晰,并感谢您提前寻求帮助!
答案 0 :(得分:0)
To order your items, leave it to Firebase. For example say that you want the transactions to be ordered on creation date, you could:
var ref = fb.child("members")
.child(fbAuth.uid)
.child("accounts")
.child($scope.AccountId)
.child("transactions")
.orderByChild("creationDate");
$scope.transactions = $firebaseArray(ref);
If that does not cover what you want, please update your question to be clearer on what you're trying to sort on. Right now, I have no clue what you're trying to sort on.
I just noticed your update and that indeed clarifies what you're trying to accomplish.
In your first data snippet, the data is ordered by customindex
. So you can retrieve them in that order by:
var ref = fb.child("members")
.child(fbAuth.uid)
.child("accounts")
.child($scope.AccountId)
.child("transactions")
.orderByChild("customindex");
$scope.transactions = $firebaseArray(ref);
In your last data snippet, the transactions are no longer ordered by any property. So when the user drags the Starbucks transaction from its last place and drops it in second place, you'll need to update the customindex
property of all transactions after it. If you update those values, AngularFire will automatically ensure that the items are in the correct order in the $firebaseArray
again.
Note that this requires updating 4 transactions, which may not be very efficient. Alternatively you can:
transaction_order
node.customindex
to 25: [10,20,25,30,40,50]. This approach is a bit hacky, but is simpler to implement and will normally work pretty well.