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

Is it possible to use page and size while creating datasource #46

Closed
andiwonder opened this issue May 30, 2018 · 4 comments
Closed

Is it possible to use page and size while creating datasource #46

andiwonder opened this issue May 30, 2018 · 4 comments

Comments

@andiwonder
Copy link

Hi,

My api is build using the params page and size , so I can do something like page 1 and size 10 to get elements 11 to 20. I was wondering if I could create a datasource with those params or is it fixed to index and count ?

@dhilt
Copy link
Owner

dhilt commented May 30, 2018

Hey, thanks for the issue! The Datasource.get method has only index-count signatures. But you can implement pages logic as an intermediate layer between the Datasource (uiScroll) and the Remote (server data). Something like

get: (index, count, success) => {
  const start = Math.max(index, 0); // no negative indexes 
  const end = start + count;
  const startPage = Math.floor(start / count);
  const endPage = Math.ceil(end / count);
  // ... request data for pages from startPage to endPage
    // ... "result" should have joined array of requested items
    const data = result.slice(start - startPage * count, start - startPage * count + count);
    success(data);
}

With this approach we do some extra work and should throw away items that are out of real index-count scope (result.slice). Depending on your needs this could be improved. For example you may build a full cache layer here: all data is being stored in special array (Cache, see below) that could increase only, and then you slice that array to pass index-count piece into the uiScroll. If some items are not in the Cache, do a request for the page that should include missing items, then store the response items in the Cache and then slice the Cache to pass index-count piece into the uiScroll.


interface IDatasourceCacheItem {
  index: number; // should be relevant to the Datasource.get index
  item: any;
}
...
Cache: List<IDatasourceCacheItem>;

There are many ways and things that could be done: page size can differ from the Datasource.get count parameter (which is bufferSize setting), also, you may take into the account max page number value etc. Datasource.get method implementation could be very flexible and complicated, I hope I was able to reflect the idea of additional layer within the "get" method.

@MasterToney
Copy link

@dhilt have you considered adding a minIndex setting at one point?
Asking because I too limit the index to 0 and thought that it would be neat if you could set that just like the startIndex for example.
I suspect that this use case might be quite common ;)
Keep up the good work!

@dhilt
Copy link
Owner

dhilt commented May 30, 2018

@MasterToney Sure! I understand the need and AngularJS version had such a setting. It would be nice if you create a separate issue for minIndex setting, because this one will be closed (I hope).

@andiwonder
Copy link
Author

Hey, thanks for the explanation. Was able to get it working (sort of) with your code and you can close the issue if you want. Thanks for the reply.

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

3 participants