Fireship · @fireship

Simple Pagination with Firebase and Angular 4

Uploaded June 30, 2017 · Length 4:20

Simple Pagination with Firebase and Angular 4

Summary

build simple pagination with firebase and angular fire, navigating through comments with next and previous buttons

  • firebase pagination
  • angular fire
  • pagination in angular
  • firebase comments
  • angular pagination

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:00

    and this episode we're going to build a

  2. 0:02

    simple pagination feature using firebase

  3. 0:05

    and angular fire to package this

  4. 0:08

    solution allows the user to click a next

  5. 0:10

    or previous button to navigate through a

  6. 0:13

    firebase to list observable the benefit

  7. 0:16

    of this approach is that it can be

  8. 0:17

    applied to a data set of any size but

  9. 0:20

    the drawback is that it's impossible to

  10. 0:22

    really implement page numbers for

  11. 0:24

    pagination in the database we have a

  12. 0:26

    series of common nested under their

  13. 0:29

    associated post ID let's start by

  14. 0:33

    retrieving a firebase list observable

  15. 0:35

    from the service first we import the

  16. 0:37

    angular fired database and then we build

  17. 0:40

    a function that will take a ghost ID an

  18. 0:44

    offset which is the number of comments

  19. 0:46

    we want to retrieve and a start key

  20. 0:48

    which is where we'll start the actual

  21. 0:50

    query in firebase we can't use a numeric

  22. 0:54

    offset like you would in a sequel

  23. 0:55

    database so we keep track of the key and

  24. 0:58

    use that as the offset point for each

  25. 1:00

    query the key for the next page is

  26. 1:03

    determined by grabbing one extra comment

  27. 1:05

    from the database and we use that

  28. 1:07

    comments key is the offset point for the

  29. 1:09

    following page

  30. 1:11

    [Music]

  31. 1:15

    now we can start building the component

  32. 1:17

    in the template we're going to loop

  33. 1:20

    through a list of comments but notice

  34. 1:22

    we're not using the async pipe because

  35. 1:24

    we're actually going to unwrap the

  36. 1:26

    observable in the component type script

  37. 1:28

    we also had a couple buttons just for

  38. 1:31

    navigating between the next and previous

  39. 1:33

    pages we also want to disable these

  40. 1:36

    buttons if there isn't a next or

  41. 1:39

    previous page to navigate to in the type

  42. 1:44

    script we start by injecting the

  43. 1:46

    comments service and we also import

  44. 1:48

    lodash which we'll be using to do a lot

  45. 1:50

    of the array operations in this

  46. 1:52

    implementation the comments variable

  47. 1:55

    will hold the actual comments from the

  48. 1:57

    observable the offset will be the number