使用实体服务 API 进行排序和分页
¥Ordering and Paginating with the Entity Service API
Strapi v5 中的实体服务 API 已弃用。请考虑改用 文档服务 API。
¥The Entity Service API is deprecated in Strapi v5. Please consider using the Document Service API instead.
实体服务 API 能够提供通过 findMany() 方法找到的 order 和 paginate 结果。
¥The Entity Service API offers the ability to order and paginate results found with its findMany() method.
顺序
¥Ordering
要对实体服务 API 返回的结果进行排序,请使用 sort
参数。结果可以基于 single 或 multiple 属性排序,也可以使用 关系排序。
¥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
以默认升序排序,或者¥as a
string
to sort with the default ascending order, or -
作为
object
来定义字段名称和顺序(即'asc'
表示升序或'desc'
表示降序)¥as an
object
to define both the field name and the order (i.e.'asc'
for ascending order or'desc'
for descending order)
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:
-
作为字符串数组,使用默认升序对多个字段进行排序,或者
¥as an array of strings to sort multiple fields using the default ascending order, or
-
作为定义字段名称和顺序的对象数组(即
'asc'
表示升序或'desc'
表示降序)¥as an array of objects to define both the field name and the order (i.e.
'asc'
for ascending order or'desc'
for descending order)
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 返回的结果进行分页,可以使用 start
和 limit
参数:
¥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,
});
你可以改为使用 page
和 pageSize
参数:
¥You may instead use the page
and pageSize
parameters:
strapi.entityService.findMany('api::article.article', {
page: 1,
pageSize: 15,
});