Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to get custom data per page #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

KillerDom1123
Copy link
Collaborator

TODO: Unit tests

These changes add the ability to get "custom data" per page.

This is useful for cases where you may need to send and get specific data from an API. Currently GridJS does not completely support this, the best we get is limit/offset pagination.

For example, this feature allows you to do this

const getData = async (page, limit) => {
  // This could be a call to your API where you can send specific data based on the page/limit
  const data = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
    [10, 11, 12],
  ];

  const start = page * limit;
  const end = (page + 1) * limit;

  return data.slice(start, end);
};

useEffect(() => {
  grid = new Grid({
    columns: ['a', 'b', 'c'],
    pagination: {
      limit: 2,
      forceTotal: 20,
      customData: (page, limit) => getData(page, limit),
    },
    data: [],
  }).render(ref.current);
});

When you paginate getData gets called with the relevant arguments.

To use this, you need to set forceTotal and customData in the pagination option. It's best to keep data as an empty array, such as above.

customData will need to return a Promise.
forceTotal should be the total number of results that is expected.

@Jaleeh
Copy link

Jaleeh commented Oct 23, 2023

what problem/situation is this for? filtering for serverside pag?

@KillerDom1123
Copy link
Collaborator Author

what problem/situation is this for? filtering for serverside pag?

Kinda yeah. Say you call an API with some set data which you are also showing on the page. When you paginate you can then call that API with the new data on that page.

I think this can be used with the search_transaction_summary endpoint for example - https://mypebble.readme.io/reference/transactionssearch-transactions-summary

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.

2 participants