项目作者: noook

项目描述 :
A Vue Composition plugin that handles reactive pagination and HTTP requests with filters
高级语言: TypeScript
项目地址: git://github.com/noook/vue-composition-paginate.git
创建时间: 2020-04-28T11:21:23Z
项目社区:https://github.com/noook/vue-composition-paginate

开源协议:

下载


npm version

All Contributors

npm bundle size
npm

Vue Composition Paginate

Introduction

vue-composition-pagination is a ready to use composition-api
composable tool around axios to help you dealing with paginated APIs.

It is fully written in Typescript and supports types out of the box.

Installation

npm

  1. npm install vue-composition-paginate

yarn

  1. yarn add vue-composition-paginate

Usage

Given the following payload:

  1. {
  2. "data": [
  3. {
  4. "name": {
  5. "title": "Miss",
  6. "first": "Ninon",
  7. "last": "Philippe"
  8. },
  9. "email": "ninon.philippe@example.com",
  10. "id": {
  11. "name": "INSEE",
  12. "value": "2NNaN81837684 25"
  13. }
  14. },
  15. {
  16. "name": {
  17. "title": "Mr",
  18. "first": "Axel",
  19. "last": "Skavhaug"
  20. },
  21. "email": "axel.skavhaug@example.com",
  22. "id": {
  23. "name": "FN",
  24. "value": "11118811382"
  25. }
  26. },
  27. {
  28. "name": {
  29. "title": "Monsieur",
  30. "first": "Enrico",
  31. "last": "Aubert"
  32. },
  33. "email": "enrico.aubert@example.com",
  34. "id": {
  35. "name": "AVS",
  36. "value": "756.2544.6409.51"
  37. }
  38. }
  39. // ...
  40. ],
  41. "pagination": {
  42. "page": 1,
  43. "total": 624,
  44. "resultsPerPage": 25,
  45. "totalPage": 25
  46. }
  47. }
  1. import usePaginate from 'vue-composition-paginate';
  2. import myAxiosInstance from '@/utils/axios-instance';
  3. export default defineComponent({
  4. name: 'ListView',
  5. setup() {
  6. const paginate = usePaginate({
  7. // It can also be the basic instance of axios
  8. instance: myAxiosInstance,
  9. url: 'http://api.project.local/documents', // Or '/documents' if your instance has a prefix
  10. // Extract data from payload
  11. dataTransformer: (response) => response.data,
  12. // Extract total of pages from the payload
  13. totalPageTransformer: (response) => response.pagination.totalPage,
  14. });
  15. // Initiate the first call
  16. paginate.goToPage(1);
  17. return {
  18. // See below for details about this object
  19. ...paginate,
  20. };
  21. },
  22. });

If you are using Typescript, you can also indicate through generics
the type of the payload:

  1. import myAxiosInstance from '@/utils/axios-instance';
  2. interface User {
  3. id: {
  4. name: string;
  5. value: string;
  6. };
  7. name: {
  8. title: string;
  9. first: string;
  10. last: string;
  11. };
  12. email: string;
  13. }
  14. interface Payload {
  15. data: User[];
  16. pagination: {
  17. total: number;
  18. resultsPerPage: number;
  19. page: number;
  20. totalPage: number;
  21. };
  22. }
  23. export default defineComponent({
  24. name: 'ListView',
  25. setup() {
  26. // You will be able to benefit from type inference on properties of the `paginate` object
  27. // and `usePaginate` options.
  28. const paginate = usePaginate<User, Payload>({
  29. instance: myAxiosInstance,
  30. url: 'http://api.project.local/documents',
  31. dataTransformer: (response) => response.data,
  32. totalPageTransformer: (response) => response.pagination.totalPage,
  33. });
  34. paginate.goToPage(1);
  35. return {
  36. // See below for details about this object
  37. ...paginate,
  38. };
  39. },
  40. });

Options

Name Type Required Default Description
instance AxiosInstance true Axios instance used to make requests
url String true URL of the resources to fetch. If you use a custominstance with a prefix, you can just use the resource path /documents for example.
totalPageTransformer (payload: Payload) => number true Function called to extract the total number of pages out of the payload
dataTransformer (payload: Payload) => T[] false (results) => results Function called to extract the paginated results data out of the payload
totalTransformer (payload: Payload) => number false () => {} Function called to extract the total number of items out of the payload
pageField String false "page" Name of the field in the query to specify the page we want to retrieve
onUpdate `(page?: number, params?: Record<string, string number boolean>) => void` false () => {} Function to call everytime the current data is updated. May be useful to update the URL query parameters or to trigger other actions.
currentPage Number false 1 Defines the current page to generate a range of pages around the current one
resultsPerPage `Ref \ number` false 25 Sets the limit of results to fetch at once
limitField String false "limit" Name of the field in the query to specify the maximum number of items we want to fetch
range Number false 5 Number of pages to display around the current one
includeLimits Boolean false true Whether to add first and last pages in the page list around the current one
params `Ref<Record<string, number \ boolean \ string>>` false {} Additional query params to add in the request. Must be a ref or a computed value, returning an object whose depth is 1.

Return values

The function will return an object that is destructurable containing the following properties:

Name Type Description
data Ref<T[]> Array of fetched results
pages Ref<number[]> Generated list of pages around the current page (ex: [1, 4, 5, <6>, 7, 8, 20])
currentPage Ref<number> Reactive reference of the current page
goToPage (page: number) => Promise<Payload> Function to call to go to a specific page. can be used to refresh the current query
previous (page: number) => Promise<Payload> Function to call to go to the previous page
next (page: number) => Promise<Payload> Function to call to go to the next page
resultsPerPage Ref<number> Reactive reference of the limit of results per page
total `Ref \ undefined` Reactive reference of the total number of items. undefined if no function to extract the total number of items is provided
lastPage Ref<number> Reactive reference of the number of the last page
loading Ref<boolean> Reactive reference of HTTP request completion state

Contributors ✨

Thanks goes to these wonderful people (emoji key):






Nathael Arki

🤔

This project follows the all-contributors specification. Contributions of any kind welcome!