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

Example for Infinite Scroller #157

Open
egloffmark opened this issue May 27, 2023 · 1 comment
Open

Example for Infinite Scroller #157

egloffmark opened this issue May 27, 2023 · 1 comment

Comments

@egloffmark
Copy link

I like to use the visualizer to create an infinite-scroller for a list where its content should be extended with the next page of items when the end of the current set / page get reached.

The issue I have is when I add additional elements to the items, then it get not automatically recognized by the virtualizer. So how can I achieve this?
e.g.

@customElement('product-scroller')
export class ProductScrollerView extends View {
	
	  @state()
	  data = Array(100)
	  .fill(0)
	  .map((e, i) => {
	    return this.createElement(i);
	  });
		
	render() {
		return html`
			<lit-virtualizer
				.items=${this.data}
				.renderItem=${(item : any) => html`<div><b>${item.name}</b></div>`}
				@visibilityChanged=${this.visibilityChanged}
			>
			</lit-virtualizer>
		`;
	}
	
	visibilityChanged( e: VisibilityChangedEvent) {
		console.log("first:" + e.first + " ,last: " + e.last);
		if (e.last == this.data.length - 1) {
			console.log("end reached!!!");
                        // this is not working, virtualizer seems not to get notified about that the items length has changed
                        // what has here to be changed? 
			for (let i = e.last; i < e.last + 100; i++) {
			  	this.data.push(this.createElement(i));
			}
			console.log(this.data);
		}
	}
	
	createElement(id : any) {
		return {
	      name: `Item ID: ${id}`
	    };
	}
}
@egloffmark egloffmark changed the title Example for Pagination Example for Infinite Scroller May 29, 2023
@badlogic
Copy link

badlogic commented Dec 1, 2023

You need to create a new array and assing that to the data property, e.g. this.data = [ ...this.data, ...newData]

where newData is an array only containing the items you want to append.

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