Navigation

    MemFireDB论坛

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups

    小技巧,对查询结果进行分页操作

    MemFireDB新手区
    1
    1
    11
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S
      starry last edited by starry

      你只需要一个可以为你计算范围的函数。我有一个我创建的函数,效果非常好,你可以在你的代码中使用它。

      export const getPagination = (page, size) => {
        const limit = size ? +size : 3;
        const from = page ? page * limit : 0;
        const to = page ? from + size : size;
      
        return { from, to };
      };
      

      你可以在你的查询之前添加这个,并使用返回的from和to作为你的.range属性。

      export async function getServerSideProps({ query: { page = 1 } }) {
        const { from, to } = getPagination(page, 10);
        const { data, count } = await supabase
          .from("cars")
          .select("*", { count: "exact" })
          .order("id", { ascending: true })
          .range(from, to);
          
        return {
          props: {
            data: data,
            count: count,
            page: +page,
          },
        };
      }
      

      详见:https://github.com/supabase/supabase/discussions/1223

      1 Reply Last reply Reply Quote 1
      • First post
        Last post