Pagination with SvelteKit

แชร์
ฝัง
  • เผยแพร่เมื่อ 6 เม.ย. 2023
  • This video isn't meant to be a follow-along video, but moreso a fast-paced explainer to give you some ideas of a couple different ways to you could handle pagination with SvelteKit & Prisma or any other 3rd party API. The first and most straightforward way is to follow suite with most API designs by using a couple search params (limit & skip). Another way is to use params for dynamic pages using only the page param number.
    If you find my content useful and want to support the channel, consider contributing a coffee ☕: hbyt.us/coffee
    📁 Final Source Code: github.com/huntabyte/sk-pg
    🚀 Become a channel member: hbyt.us/join
    💬 Discord: hbyt.us/discord
    🐦 Twitter: hbyt.us/twitter
    🖥️ Setup Stuff: hbyt.us/gear
    📃 Topics Covered:
    - SvelteKit Pagination
    - SvelteKit Load Function
    - Prisma Pagination
    - SvelteKit Queries
    - SvelteKit Page Numbers
    - Svelte Pagination
    - Prisma Paging
    - SvelteKit Pages
    - SvelteKit Search Params

ความคิดเห็น • 56

  • @aswinzero
    @aswinzero ปีที่แล้ว +8

    Bruh are u reading my mind, I've been doing pagination in sveltekit for almost 4 days and u dono how much I'm happy when i got this notification ❣️

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว +1

      I’m glad to hear that! There are some great pagination components to go along with this out there. Look up ‘Pagination svelte repl’ on Google for some ideas which may help you out a bit more!

    • @Allformyequine
      @Allformyequine ปีที่แล้ว

      Omgosh same here LOL!! I had to make my own weird version ... till now!!! Thank you!

  • @dheerajs2838
    @dheerajs2838 ปีที่แล้ว +3

    In continuation of our tradition, here is my comment on how great your video is :)

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว

      Thank you Dheeraj!

  • @H3eNr1K
    @H3eNr1K ปีที่แล้ว +3

    Recently you just come up with everything I need to come forward in my project, thank you so much

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว

      You’re very welcome!

  • @dimitrisplastaras3026
    @dimitrisplastaras3026 ปีที่แล้ว +6

    Always the most usefull topics.

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว +1

      Thanks Dimitris!

  • @patricknelson
    @patricknelson ปีที่แล้ว +3

    This is useful even for me; I’ve built all this stuff before over and over and over again… but in different stacks, frameworks & languages. It’s super interesting to see how someone does it in SvelteKit vs. Laravel or something.

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว +1

      I’m glad to hear that!

  • @codewithguillaume
    @codewithguillaume ปีที่แล้ว +1

    Again, an amazing video thanks !

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว

      You’re welcome, Guillaume!

  • @Solanaceously
    @Solanaceously ปีที่แล้ว

    I'm convinced you and Joy Of Code conspired to talk about pages today

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว +1

      maybe we did maybe we didn’t 🫣😂

  • @Allformyequine
    @Allformyequine ปีที่แล้ว +1

    AWESOME!!! Was hoping some knowledgeable SvelteKit guru would make a nice Pagination video!!

  • @adimardev1550
    @adimardev1550 8 หลายเดือนก่อน +1

    Thanks mate! very helpful 👌

  • @nyashachiroro2531
    @nyashachiroro2531 ปีที่แล้ว +2

    As always great useful Svelte content

  • @danielk.9321
    @danielk.9321 ปีที่แล้ว +2

    Good video, very useful

  • @kvetoslavnovak423
    @kvetoslavnovak423 ปีที่แล้ว

    Great video as usual! Any tips concerning the best SvelteKit way how to do Load more?

  • @adamrichardlimb
    @adamrichardlimb 7 หลายเดือนก่อน

    Very nice, great video as always! Do you have any plans to do infinite scrolling/lazy loading? I imagine you could do something similar to this, but when the user reaches the top/bottom of the element - ask for more data and put it into an array, saving the highest and lowest element you have retrieved from the DB to use as the starting position for future queries. I've never implemented this, so I don't know if Svelte slows down when you have that many elements on the page, especially if they're complicated.

  • @marcoantonio7648
    @marcoantonio7648 ปีที่แล้ว +1

    almost died in the sql with offset
    But great video

  • @Dev_dev_dev
    @Dev_dev_dev ปีที่แล้ว +1

    Great video! Any tips/resources on infinite scroll, or in other words appending the new page data on reaching a certain scroll point? We can't rerun "load," and if I'm using something like Prisma, I can't expose my DB by recreating the pagination in a function on the client side.

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว +3

      Cursor-based pagination would be a better fit for infinite scroll. Since this is a client side JS requirement, I’d probably setup a separate endpoint and issue fetch requests as you scroll just like you would with any other framework! Using {#await } along the way!

  • @oscarvasquez5033
    @oscarvasquez5033 ปีที่แล้ว

    Thanks!

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว

      You're welcome!

  • @mikebit5497
    @mikebit5497 ปีที่แล้ว

    How would that work if you have data put in by the user that should persist through the different pages? Like a list of favorite songs the user can select? Do they persist if the user changes the page or will they get reset when after the reload?

  • @Trav164
    @Trav164 ปีที่แล้ว

    Do you have an example of including an optional page size with a select or something?
    After giving the user the option to change page size, I'm having difficulty refreshing the data or fetching the extra entities (example: going from pageSize: 10 => 50) Until I refresh the url params this doesn't work as it only updates the pagination links

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว +1

      When you update the pages, you should redirect the user to /whatever?limit=.

    • @Trav164
      @Trav164 ปีที่แล้ว

      @@Huntabyte if you’ve got some sort of filter on the table, how would you persist that if you’re redirecting users though?

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว +1

      @@Trav164 through the search params :)

  • @Loui3Hunna
    @Loui3Hunna ปีที่แล้ว

    MVP!!

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว

      Thank you thank you!

  • @mwlo8635
    @mwlo8635 7 หลายเดือนก่อน

    Why are totalPages, currentPage, totalItems etc reactive? Wouldn’t that also work as normal variable?

  • @danielrios8037
    @danielrios8037 ปีที่แล้ว

    So easy explained, thanks a lot! Would it be possible to have a master detail mvp ?

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว

      What do you mean by master detail MVP?

    • @danielrios8037
      @danielrios8037 ปีที่แล้ว

      @@Huntabyte a minimal example of a master detail crud

  • @wandenreich770
    @wandenreich770 4 หลายเดือนก่อน

    Hmm I sort have an issue with this example cause the /products/page conflicts with /products/productId in a real life scenario .I know this wasnt your intention but you could maybe highlight it or something

  • @kupyn
    @kupyn ปีที่แล้ว

    so what if i also have a serach bar? because right now i show paginated items, but if search query is in place i show all users

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว

      You would need some sort of conditional logic within the load function. Something to determine if it’s search to get the first X search results.

  • @tomich20
    @tomich20 ปีที่แล้ว

    thanks. My problem is when filtering, if i use the skip 30 param because im on page 3, i wont be getting the data i wanted.

    • @tipeon
      @tipeon ปีที่แล้ว

      Are you filtering your data in Javascript *after* the database query ?
      If so ... don't do that. Add a WHERE clause to your SQL query

    • @tomich20
      @tomich20 ปีที่แล้ว

      @@tipeon filtering on the query directly.. but if i do "select * from users where name like "%tom%" offset 10;" i will be missing the first 10 toms of the list, so the offset should be reset on every filtering?..?

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว

      I think in that case you would want to have an optional param that change the data retrieved from the DB *if* a filter is applied. Can certainly be done!

  • @thirupparan421
    @thirupparan421 ปีที่แล้ว +2

    your videos are amazing. would you explain pagination page logic first show 5 page and after 5?

  • @theshy6717
    @theshy6717 ปีที่แล้ว

    Love the font, what's the name of it?

  • @adrianfr05
    @adrianfr05 ปีที่แล้ว

    why is the path on 3:46 not written with backticks and the variables with a $???

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว +1

      Svelte Magic 🪄- you don’t need to include backticks or $ inside of attributes in Svelte components!

  • @doemaeries
    @doemaeries 5 หลายเดือนก่อน

    might be a stupid question, but why do you use +page.ts and not +page.server.ts?

    • @Huntabyte
      @Huntabyte  5 หลายเดือนก่อน +1

      Wasn't hitting a private API in this case so there was no reason to make the trip to the server every time.

  • @tipeon
    @tipeon ปีที่แล้ว +1

    Erm .... You're using offset and limit *without* an order by clause. 😮
    Are you insane or does prisma adds it for you under the hood ?

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว

      Just a quick example 😂 I want to say by default it sorts by PK but I could be wrong there!

    • @TechBuddy_
      @TechBuddy_ ปีที่แล้ว +1

      @@Huntabyte postgres and MySQL order them in the order of insertion by default
      edit: this feature is not guaranteed and might change in the future
      Edit 2: The default behaviour only applies if you only insert and never update or delete the records

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว

      ​@@TechBuddy_ Thanks for that info! Good to know, I usually orderby the field of my choosing but great info!