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

Issue: First get uses index 1, whereas array starts at index 0 #44

Closed
jesperzach opened this issue Feb 11, 2016 · 11 comments
Closed

Issue: First get uses index 1, whereas array starts at index 0 #44

jesperzach opened this issue Feb 11, 2016 · 11 comments

Comments

@jesperzach
Copy link

Is there any reasoning behind this?

The problem is ui scroll starts loading from index 1, then shortly after prepends the element at index 0. This results in the container being scrolled down slightly on initial page load.

I tried just subtracting 1 from index before fetching elements from my array, but this can result in a situation where the requested index is -10 and count is 10 (total 0 elements requested) which means it will stop asking for more.

@mfeingold
Copy link
Contributor

Is there an item with index 0 in your dataset? If there is not, it should not prepend anything there.

@ribeirobreno
Copy link

Like me, he might have tried to map his dataset into a standard Javascript Array.

In my use case, the data is retrieved from a web service which returns a JSON string that is parsed into a standard JS Array. This means that the dataset index is zero based and it will always have an item with index 0, even if i don't use it.

@jesperzach
Copy link
Author

Yes, I'm using standard 0-based javascript arrays. Should I not?

@mfeingold
Copy link
Contributor

You can use anything - 0 based arrays included, it does not change the logic on what items are sent back in the responses. Assuming the you have and items[20] array the scroller is going to call get as follows:
Request 1 start:1 count: 10 - you have to respond with 10 items 1 through 10
Request 2 start: 11 count 10 - respond with 9 items 11 through 19
Request 3 start: -9 count 10 - respond with 1 item 0

Keep in mind that as you scroll up and down the start index can change to pretty much anything, so the math selecting the array subset although simple is somewhat confusing. You can check the examples i.e. this one

@ribeirobreno
Copy link

I just decremented the index before doing any selection to respond.

So, for an array with length==20 :
Request 1 start: 1 count: 10 - you have to respond with 10 items from index 0 through 9
Request 2 start: 11 count 10 - respond with 10 items from index 10 through 19
Request 3 start: -9 count 10 - respond with 0 items

What i don't understand is really why?

@mfeingold
Copy link
Contributor

Yea, it should work. You just should also be prepared to handle the requests with any start index between -9 and 20

@ribeirobreno
Copy link

Just for completeness and considering the array has all the data:

--index; // ...
endItem = index + count;
startItem = (index<0?0:index);

this.data.slice(startItem, endItem)

@jesperzach
Copy link
Author

Yes this is exactly the solution I came up with, however, it doesn't work with all viewport sizes.
The problem is on small viewports it would ask for index -9 to 1 on the first request, after which decrementing by 1 would result in a return of index range -10 to 0. That gives a result of length 0, which causes ui scroll to stop listening for any more scroll events. That left me with an empty virtual list and no way to populate it.

@mfeingold
Copy link
Contributor

You also need to account for the case when index+count goes out of bounds

@jesperzach
Copy link
Author

But what do I do in that case?
If the problem was as trivial as the answers currently provided in this issue assume, I would have come up with a solution weeks ago.
How do I account for when index + count goes out of bounds without returning 0 items or returning duplicate items? Can you give an example?

@mfeingold
Copy link
Contributor

that's what worked for me:
start = Math.max(MININDEX, actualIndex);
end = Math.min(actualIndex + count - 1, MAXINDEX);
This is taken from this example

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