Skip to main content
Unlisted page
This page is unlisted. Search engines will not index it, and only users having a direct link can access it.

使用实体服务 API 进行排序和分页

🌐 Ordering and Paginating with the Entity Service API

Caution

在 Strapi v5 中,实体服务 API 已被弃用。请考虑改用 文档服务 API

🌐 The Entity Service API is deprecated in Strapi v5. Please consider using the Document Service API instead.

实体服务 API 提供通过其 findMany() 方法找到的结果进行 排序分页 的能力。

🌐 The Entity Service API offers the ability to order and paginate results found with its findMany() method.

排序

🌐 Ordering

要对实体服务 API 返回的结果进行排序,请使用 sort 参数。结果可以基于单个多个属性进行排序,也可以使用关系排序

🌐 To order results returned by the Entity Service API, use the sort parameter. Results can be ordered based on a single or on multiple attribute(s) and can also use relational ordering.

单身

🌐 Single

要按单个字段对结果进行排序,请将其传递给 sort 参数,方法如下:

🌐 To order results by a single field, pass it to the sort parameter either:

  • 作为 string 用默认的升序排序,或者
  • 作为一个 object 来定义字段名和顺序(即 'asc' 表示升序,'desc' 表示降序)
strapi.entityService.findMany('api::article.article', {
sort: 'id',
});

// single with direction
strapi.entityService.findMany('api::article.article', {
sort: { id: 'desc' },
});

多个

🌐 Multiple

要按多个字段排序结果,请将字段作为数组传递给 sort 参数,方式如下:

🌐 To order results by multiple fields, pass the fields as an array to the sort parameter either:

  • 作为字符串数组,使用默认升序对多个字段进行排序,或者
  • 作为对象数组来定义字段名称和顺序(即升序为 'asc',降序为 'desc'
strapi.entityService.findMany('api::article.article', {
sort: ['publishDate', 'name'],
});

// multiple with direction
strapi.entityService.findMany('api::article.article', {
sort: [{ title: 'asc' }, { publishedAt: 'desc' }],
});

关系顺序

🌐 Relational ordering

字段还可以根据关系中的字段进行排序:

🌐 Fields can also be sorted based on fields from relations:

strapi.entityService.findMany('api::article.article', {
sort: {
author: {
name: 'asc',
},
},
});

分页

🌐 Pagination

要对实体服务 API 返回的结果进行分页,你可以使用 startlimit 参数:

🌐 To paginate results returned by the Entity Service API, you can use the start and limit parameters:

strapi.entityService.findMany('api::article.article', {
start: 10,
limit: 15,
});

你也可以改用 pagepageSize 参数:

🌐 You may instead use the page and pageSize parameters:

strapi.entityService.findMany('api::article.article', {
page: 1,
pageSize: 15,
});