热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

$firebaseArray将对象从索引移动到索引-$firebaseArraymoveobjectfromindextoindex

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 个解决方案

#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个事务,这可能不是非常有效。或者你可以:

  1. 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.
  2. 保留事务详细信息(您最初拥有的)和单独节点中的事务顺序。这样你只需要更新transaction_order节点。

  3. 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.
  4. 在初始索引之间保留一些空间。因此,而不是[1,2,3,4,5],以[10,20,30,40,50]开头。然后,当用户将星巴克在20到30之间移动时,您只需将星巴克交易的customindex更新为25:[10,20,25,30,40,50]。这种方法有点笨拙,但实现起来更简单,并且通常可以很好地工作。


推荐阅读
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • baresip android编译、运行教程1语音通话
    本文介绍了如何在安卓平台上编译和运行baresip android,包括下载相关的sdk和ndk,修改ndk路径和输出目录,以及创建一个c++的安卓工程并将目录考到cpp下。详细步骤可参考给出的链接和文档。 ... [详细]
  • 计算机存储系统的层次结构及其优势
    本文介绍了计算机存储系统的层次结构,包括高速缓存、主存储器和辅助存储器三个层次。通过分层存储数据可以提高程序的执行效率。计算机存储系统的层次结构将各种不同存储容量、存取速度和价格的存储器有机组合成整体,形成可寻址存储空间比主存储器空间大得多的存储整体。由于辅助存储器容量大、价格低,使得整体存储系统的平均价格降低。同时,高速缓存的存取速度可以和CPU的工作速度相匹配,进一步提高程序执行效率。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 本文介绍了如何使用Express App提供静态文件,同时提到了一些不需要使用的文件,如package.json和/.ssh/known_hosts,并解释了为什么app.get('*')无法捕获所有请求以及为什么app.use(express.static(__dirname))可能会提供不需要的文件。 ... [详细]
  • 本文介绍了在CentOS上安装Python2.7.2的详细步骤,包括下载、解压、编译和安装等操作。同时提供了一些注意事项,以及测试安装是否成功的方法。 ... [详细]
  • 本文介绍了一个适用于PHP应用快速接入TRX和TRC20数字资产的开发包,该开发包支持使用自有Tron区块链节点的应用场景,也支持基于Tron官方公共API服务的轻量级部署场景。提供的功能包括生成地址、验证地址、查询余额、交易转账、查询最新区块和查询交易信息等。详细信息可参考tron-php的Github地址:https://github.com/Fenguoz/tron-php。 ... [详细]
  • 本文讨论了在VMWARE5.1的虚拟服务器Windows Server 2008R2上安装oracle 10g客户端时出现的问题,并提供了解决方法。错误日志显示了异常访问违例,通过分析日志中的问题帧,找到了解决问题的线索。文章详细介绍了解决方法,帮助读者顺利安装oracle 10g客户端。 ... [详细]
  • 使用nodejs爬取b站番剧数据,计算最佳追番推荐
    本文介绍了如何使用nodejs爬取b站番剧数据,并通过计算得出最佳追番推荐。通过调用相关接口获取番剧数据和评分数据,以及使用相应的算法进行计算。该方法可以帮助用户找到适合自己的番剧进行观看。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • javascript  – 概述在Firefox上无法正常工作
    我试图提出一些自定义大纲,以达到一些Web可访问性建议.但我不能用Firefox制作.这就是它在Chrome上的外观:而那个图标实际上是一个锚点.在Firefox上,它只概述了整个 ... [详细]
  • 本文介绍了Oracle数据库中tnsnames.ora文件的作用和配置方法。tnsnames.ora文件在数据库启动过程中会被读取,用于解析LOCAL_LISTENER,并且与侦听无关。文章还提供了配置LOCAL_LISTENER和1522端口的示例,并展示了listener.ora文件的内容。 ... [详细]
  • 配置IPv4静态路由实现企业网内不同网段用户互访
    本文介绍了通过配置IPv4静态路由实现企业网内不同网段用户互访的方法。首先需要配置接口的链路层协议参数和IP地址,使相邻节点网络层可达。然后按照静态路由组网图的操作步骤,配置静态路由。这样任意两台主机之间都能够互通。 ... [详细]
  • 本文介绍了OpenStack的逻辑概念以及其构成简介,包括了软件开源项目、基础设施资源管理平台、三大核心组件等内容。同时还介绍了Horizon(UI模块)等相关信息。 ... [详细]
author-avatar
520那孩HAPPY
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有