fakejack
fakejack

Reputation: 21

Is there a way to get total items using ApiPlatform Pagination?

I'm trying to paginate a table client-side using bootstrap-vue. The api resource i'm trying to paginate actually counts 44 items.

The bootstrap-vue pagination component expects a total-row prop in order to get total pages number (based on items per page) the /resource GET response return hydra:totalItems: 15

15 is actually the default itemsPerPage i have in App/config/packages/api_platform.yaml configuration, which is the following:

collection:
exists_parameter_name: 'exists'
order: 'DESC'
order_parameter_name: 'order'
pagination:
  enabled: true
  partial: true
  client_enabled: true
  client_items_per_page: true
  client_partial: true
  items_per_page: 15
  maximum_items_per_page: 100
  page_parameter_name: 'page'
  enabled_parameter_name: 'pagination'
  items_per_page_parameter_name: 'itemsPerPage'
  partial_parameter_name: 'partial'

I get the same result both with partial=true/false and ?itemsPerPage=x. The pagination itself is working: if i call for ?page=1 or ?page=2 i get correct items but hydra:totalItems always refers to items returned based on itemsPerPage param.

Also, according to ApiPlatform docs i should also receive a hydra:view object with infos on next, prev and last item just enabling pagination on server with its defaults but it's not in the response.

Is there a way to receive paginated collections AND a total?
Current config:
ApiPlatform v. 1.2.2
Symfony 5.1

Upvotes: 1

Views: 4082

Answers (1)

Andre Bellafronte
Andre Bellafronte

Reputation: 81

I see this question more as a business logic than as an API Platform feature

I believe that the simplest way is to implement a query in the entity's object repository that counts the total number of records and returns to a property of your entity with a getter.

See how to achieve this with Symfony docs Querying for Objects: The Repository

Upvotes: 0

Related Questions