如何在Strapi中为关联字段实现分页?

huangapple go评论90阅读模式
英文:

How to implement pagination for relation field in Strapi?

问题

I'm using Strapi as my backend framework for a web application, and I have encountered an issue regarding pagination for relations. Specifically, I'm trying to implement pagination for a relation field in one of my models.

Let's say I have two models: "Category" and "Article." Each Category can have multiple Articles, and I want to paginate the Articles related to a particular user. However, I haven't been able to find a straightforward way to achieve this in Strapi.

This is client code:

 const query = {
    populate: {
      articles: true //pagination (for example get the first 10 items or second 10 items)
    }
  };
  const queryToString = qs.stringify(query, { encode: false });
  const {
    data: { data: categories },
  } = await axiosApiInstance.get(
    `${BACKEND_BASE_URL}categories/${id}?${queryToString}`
  );

Based on the documentation, The population and pagination operators cannot be combined.

Can someone guide me on how to implement pagination specifically for relation fields in Strapi?

英文:

I'm using Strapi as my backend framework for a web application, and I have encountered an issue regarding pagination for relations. Specifically, I'm trying to implement pagination for a relation field in one of my models.

Let's say I have two models: "Category" and "Article." Each Category can have multiple Articles, and I want to paginate the Articles related to a particular user. However, I haven't been able to find a straightforward way to achieve this in Strapi.

This is client code:

 const query = {
    populate: {
      articles: true //pagination (for example get the first 10 items or second 10 items)
    }
  };
  const queryToString = qs.stringify(query, { encode: false });
  const {
    data: { data: categories },
  } = await axiosApiInstance.get(
    `${BACKEND_BASE_URL}categories/${id}?${queryToString}`
  );

Based on the documentation, The population and pagination operators cannot be combined.

Can someone guide me on how to implement pagination specifically for relation fields in Strapi?

答案1

得分: 0

我在 Strapi 源代码中找到了答案。这部分内容并未包含在文档中。

我们可以直接在 populate 字段中使用 limitstart

const query = {
  populate: {
    articles: {
      sort: "title:asc",
      limit: 10,
      start: 10,
    },
  },
};
const queryToString = qs.stringify(query, { encode: false });
const {
  data: { data: categories },
} = await axiosApiInstance.get(
  `${BACKEND_BASE_URL}categories/${id}?${queryToString}`
);
英文:

I found the answer in the Strapi source code itself. it's not in the documentation.

We can use limit and start directly in the populate field.

const query = {
  populate: {
    articles: {
      sort: "title:asc",
      limit: 10,
      start: 10,
    },
  },
};
const queryToString = qs.stringify(query, { encode: false });
const {
  data: { data: categories },
} = await axiosApiInstance.get(
  `${BACKEND_BASE_URL}categories/${id}?${queryToString}`
);

huangapple
  • 本文由 发表于 2023年7月6日 16:08:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76626760.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定