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 an example for Pagination #163

Open
chsanch opened this issue May 2, 2023 · 2 comments
Open

Add an example for Pagination #163

chsanch opened this issue May 2, 2023 · 2 comments

Comments

@chsanch
Copy link

chsanch commented May 2, 2023

I've started using this library to implement a Table that fetches data from a Supabase API, so far I've implemented a very basic table, I've added TailwindCSS and I'm using ember-resources of handle remote data, what I don't really get (at least not from the documentation) is how to implement Pagination.
It would be really useful to see a simple example on how to get started, I know there is the API doc for the Interface but maybe a simple example would be better for newcomers to easily implement it (as the other examples already provided).

@NullVoxPopuli
Copy link
Contributor

NullVoxPopuli commented May 2, 2023

the Pagination type is actually a legacy thing 😅 https://github.com/search?q=repo%3ACrowdStrike%2Fember-headless-table%20Pagination&type=code <- no usages

it should probably be removed, tbh.

Pagination is a UI + Data-management concern, which is outside the socpe of ember-headless-table (you manage all data and presentation concerns).

So, getting to your question though: how would you do pagination?

<YourDataProvider as |api|>

  <table>
    ... headless table usage here
    <tbody>
      {{#each api.data}}

      {{/each}}
    </tbody>
  </table>

  Your Pagination component
  <Pagination @next={{api.nextPage}} @goToPage={{api.goToPage}} />
</YourDataProvider>

example of YourDataProvider

class extends Component {
  @tracked page = 0;

  @use data = keepLatest({
    when: () => this.request.isLoading,
    value: () => this.request.value ?? [],
  });
  @use request = RemoteData(() => `/api/some-endpoint?page=${this.page})`;

  nextPage = () => this.page++;
  goToPage = (num) => this.page = num;
}
{{yield (hash 
  data=this.data.value 
  nextPage=this.nextPage 
  goToPage=this.goToPage 
)}}
or in gjs

(idk if this is a good idea yet)
idk if this'll work either -- just tossing it out there.
I should probably make it work tho 🤔

const YourDataProvider = resource(() => {
  let page = cell(0);
  
  let request = use(RemoteData(() => `/api/some-endpoint?page=${page.current}`));
  let data = use(keepLatest({ value: () => request.value, when: () => request.isLoading }));

  return {
    get data() { return data.value },
    nextPage: () => page.current++,
    goToPage: (num) => page.set(num),
  };
});

<template>
  {{#let (YouDataProvider) as |api|}}

  ...

  Your Pagination component
  <Pagination @next={{api.nextPage}} @goToPage={{api.goToPage}} />

</template>

🤷

I 100% agree that a pagination example should be present on the docs site though!

A PR would be greatly welcome <3

@chsanch
Copy link
Author

chsanch commented May 2, 2023

Thanks for the clarification, I will see how I can implement this on my own with your explanation, and I will try to get a working example to see if can be useful as an example on the docs.

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

No branches or pull requests

2 participants