AngularJS:取记录,从火力地堡分页 [英] AngularJS : Fetch records for pagination from Firebase

查看:194
本文介绍了AngularJS:取记录,从火力地堡分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现使用AngularJS与火力地堡作为后端分页。我使用的是低于code来从URL火力数据:

I'm trying to implement pagination using AngularJS with Firebase as back end. I'm using the below code to fetch data from firebase url:

var firebaseObj = new Firebase("<firebaseUrl>.com/Details/");

var sync = $firebase(firebaseObj.startAt($scope.username).endAt($scope.username).limitToFirst(5));

$scope.articles = sync.$asArray();

上面的查询给我的第一条记录其启动和与特定用户名结束。现在,如何得到下一组数据从指数6至10。
任何帮助将是AP preciated ...

The above query gives me the first 5 records which start and end with the particular username. Now how to get the next set of data from index 6 to 10. Any help would be appreciated ...

推荐答案

有在火力地堡无偏移功能,当你开始神交实时这有一定道理,协作方面(偏移在一个没有实际意义流体数据集)。

There is no offset function in Firebase, which makes some sense when you begin to grok the real-time, collaborative aspects (an offset has no real meaning in a fluid data set).

回答你的具体问题实际上是因为它听起来那么简单。该算法是一种基本的光标战略,将是这个样子:

The answer to your specific question is actually as simple as it sounds. The algorithm is a rudimentary cursor strategy, and would look something like this:


  • 找到从当前数据集的最后一个键

  • 使用startAt并限制抓住下一组(页数加1)

  • 放弃第一个关键

  • 申请数据,以$范围

这看起来像下面这样:

function getLastKey(obj) {
    return Object.keys(obj).pop();
}

function nextPage() {
   var lastKey = Object.keys($scope.articles);
   // stop listening to the old data
   $scope.articles.$destroy();
   var ref = firebaseObj.orderByKey()
      .startAt(lastKey)
      // 5 + 1 to account for the lastKey being in the results
      .limitToFirst(6);
   // sync to the new results
   $scope.articles = $firebase(ref).$asArray();
}

在实践中,有很多需要做更多的工作,使分页工作在优雅角。

In practice, there's a lot more work to be done to make pagination work elegantly in Angular.

最明显的就是,我们需要有跳过第一个记录的一些手段。 presumably我们会知道的页码,因此可以从NG-重复筛选出的第一个项目,一个自定义的过滤功能。

The most obvious of which is that we need to have some means of skipping the first record. Presumably we'd know the page number and could therefore filter out the first item from ng-repeat with a custom filter function.

此外,检查出这个的jsfiddle呈现出简洁PAGINATE类

这篇关于AngularJS:取记录,从火力地堡分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆