作者:520那孩HAPPY | 来源:互联网 | 2023-05-19 00:44
HereIpullalistoftransactionsandIshowtheminanion-listwithshow-reorderenabled在这里,我提取了
Here I pull a list of transactions and I show them in an ion-list with show-reorder enabled
在这里,我提取了一个事务列表,并在启用了show-reorder的离子列表中显示它们
var ref = fb.child("/members/" + fbAuth.uid + "/accounts/" + $scope.AccountId + "/transactions/");
$scope.transactiOns= $firebaseArray(ref);
I have the following code to handle sorting
我有以下代码来处理排序
$scope.moveItem = function (transaction, fromIndex, toIndex) {
$scope.transactions.splice(fromIndex, 1);
$scope.transactions.splice(toIndex, 0, transaction);
};
I know I'm approaching this the wrong way from reading the docs here. However, I have not been able to find the way to sort items (transactions in my case) and save those changes back to Firebase. Can you help?
我知道我正在通过阅读这里的文档来解决这个问题。但是,我无法找到对项目进行排序的方法(在我的情况下是事务)并将这些更改保存回Firebase。你能帮我吗?
UPDATE
Based on Frank van Puffelen comments below, I'm adding additional info. Take the following transactions as an example. I pull the following transactions from Firebase and I order them by "customIndex". So if I "move" customindex: "5" (Starbucks transaction) between customindex: "1" (McDonalds) and customindex: "2" (Walmart)
根据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"}
}}
I should end up with the following data in Firebase:
我最终应该在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"}
}}
I hope this helps making it more clear and thank you in advanced for your help!
我希望这有助于使其更清晰,并感谢您提前为您提供帮助!
1 个解决方案
To order your items, leave it to Firebase. For example say that you want the transactions to be ordered on creation date, you could:
要订购商品,请将其留给Firebase。例如,假设您希望在创建日期订购交易,您可以:
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.
如果这不能满足您的需求,请更新您的问题,以便更清楚地了解您要排序的内容。现在,我不知道你想要点什么。
Update
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:
在您的第一个数据代码段中,数据按customindex排序。因此,您可以按以下顺序检索它们:
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.
在您的上一个数据代码段中,任何属性都不再对事务进行排序。因此,当用户从最后一个位置拖动星巴克交易并将其放在第二位时,您需要更新之后所有交易的customindex属性。如果更新这些值,AngularFire将自动确保$ firebaseArray中的项目顺序正确。
Note that this requires updating 4 transactions, which may not be very efficient. Alternatively you can:
请注意,这需要更新4个事务,这可能不是非常有效。或者你可以:
- keep the transaction details (what you initially had) and the transaction order in separate nodes. That way you'll only have to update the
transaction_order
node.
保留事务详细信息(您最初拥有的)和单独节点中的事务顺序。这样你只需要更新transaction_order节点。
- keep some space between your initial indexes. So instead of [1,2,3,4,5], start with [10,20,30,40,50]. Then when the user moves Starbucks up between 20 and 30, you can simply update the Starbucks transaction's
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.
在初始索引之间保留一些空间。因此,而不是[1,2,3,4,5],以[10,20,30,40,50]开头。然后,当用户将星巴克在20到30之间移动时,您只需将星巴克交易的customindex更新为25:[10,20,25,30,40,50]。这种方法有点笨拙,但实现起来更简单,并且通常可以很好地工作。