Skip to content

fix: return total count from paginated list APIs for accurate pagination#2380

Open
octo-patch wants to merge 1 commit into
songquanpeng:mainfrom
octo-patch:fix/issue-2338-pagination-count
Open

fix: return total count from paginated list APIs for accurate pagination#2380
octo-patch wants to merge 1 commit into
songquanpeng:mainfrom
octo-patch:fix/issue-2338-pagination-count

Conversation

@octo-patch

Copy link
Copy Markdown

Fixes #2338

Problem

The paginated list endpoints for channels, users, tokens, redemptions, and logs returned only the current page's data without a total record count. The berry-theme frontend inferred the total heuristically:

count={items.length + (items.length % ITEMS_PER_PAGE === 0 ? 1 : 0)}

This means that after loading the first page (e.g. 10 items), MUI TablePagination received count = 11, showing only 2 navigable pages at a time. Users had to click "Next" repeatedly to reach later pages — they could never jump directly to page 5 or the last page.

Solution

Backend — Add Count* functions in the model layer that execute a SELECT COUNT(*) with the same filter conditions as the corresponding Get* functions:

  • model.CountChannels()
  • model.CountUsers()
  • model.CountUserTokens(userId)
  • model.CountRedemptions()
  • model.CountAllLogs(...) (respects all filter params)
  • model.CountUserLogs(...) (respects all filter params)

Each GetAll* controller now runs the count query and includes a total field in the JSON response. This change is backward-compatible — old clients and other themes simply ignore the new field.

Frontend (berry theme) — Each paginated list view:

  1. Stores total from the API response in a dedicated state variable (channelCount, userCount, etc.)
  2. Passes it directly to TablePagination's count prop, making all pages visible immediately
  3. Updates onPaginationChange to fetch any not-yet-loaded page when the user navigates directly (e.g. jumps from page 1 to page 8)
  4. Updates search handlers to set count = data.length for search results (which are returned all at once)

Testing

  • Load the channel list with > 10 channels: pagination now shows the correct total pages immediately
  • Navigate directly to a page beyond what was initially loaded: the page data is fetched on demand
  • Perform a keyword search: pagination reflects the number of search results
  • Non-berry themes and existing API consumers are unaffected (they ignore the new total field)

…ion (fixes songquanpeng#2338)

The paginated list endpoints for channels, users, tokens, redemptions, and logs
previously returned only the current page's data without a total count. The berry
theme frontend inferred totals heuristically (data.length + 1 when the page was
full), causing TablePagination to only display 2 navigable pages at a time --
users had to click next repeatedly to reach later pages.

Changes:
- Backend: add Count* functions in model layer (CountChannels, CountUsers,
  CountUserTokens, CountRedemptions, CountAllLogs, CountUserLogs) that perform
  a SELECT COUNT(*) with the same filters as the corresponding Get* functions.
- Backend: update GetAll* controllers to run the count query and include a
  total field in the JSON response (backward-compatible; old clients ignore it).
- Frontend (berry): store total count in dedicated state, pass it to the
  TablePagination count prop so all pages are immediately visible. Update
  onPaginationChange to fetch any not-yet-loaded page on direct navigation.
  Update search handlers to set count = data.length for search results.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

切页功能只能显示默认两页,要切到最后一页只能一页一页点

1 participant