VirtualList is a placeholder that does nothing on non-JS/WASM platforms.
Destroy performs no action in non-JS/WASM builds.
{}
update recalculates the visible range and mounts the required items.
{
height := v.Container.Get("clientHeight").Int()
scrollTop := v.Container.Get("scrollTop").Int()
start := max(0, scrollTop/v.ItemHeight)
end := min(v.Total, start+height/v.ItemHeight+1)
offsetTop := start * v.ItemHeight
html := fmt.Sprintf("<div style='height:%dpx'></div>", offsetTop)
for i := start; i < end; i++ {
html += v.Render(i)
}
offsetBottom := (v.Total - end) * v.ItemHeight
html += fmt.Sprintf("<div style='height:%dpx'></div>", offsetBottom)
v.Container.SetHTML(html)
}
Destroy removes scroll listeners and cleans up resources.
{
if v.stopScroll != nil {
v.stopScroll()
}
}
NewVirtualList returns an empty VirtualList placeholder.
{
return &VirtualList{}
}
TestVirtualListStub ensures the stub implementation is non-nil and callable.
{
v := NewVirtualList("id", 10, 20, func(i int) string { return "" })
if v == nil {
t.Fatalf("expected VirtualList instance")
}
// Destroy should be a no-op and must not panic.
v.Destroy()
}
VirtualList renders only the portion of a list that is visible within its container.
Destroy performs no action in non-JS/WASM builds.
{}
update recalculates the visible range and mounts the required items.
{
height := v.Container.Get("clientHeight").Int()
scrollTop := v.Container.Get("scrollTop").Int()
start := max(0, scrollTop/v.ItemHeight)
end := min(v.Total, start+height/v.ItemHeight+1)
offsetTop := start * v.ItemHeight
html := fmt.Sprintf("<div style='height:%dpx'></div>", offsetTop)
for i := start; i < end; i++ {
html += v.Render(i)
}
offsetBottom := (v.Total - end) * v.ItemHeight
html += fmt.Sprintf("<div style='height:%dpx'></div>", offsetBottom)
v.Container.SetHTML(html)
}
Destroy removes scroll listeners and cleans up resources.
{
if v.stopScroll != nil {
v.stopScroll()
}
}
NewVirtualList attaches a virtualized list to the element with the given id.
{
v := &VirtualList{
Container: dom.Doc().ByID(containerID),
Total: total,
ItemHeight: itemHeight,
Render: render,
}
v.stopScroll = events.OnScroll(v.Container.Value, func(js.Value) {
v.update()
})
v.update()
return v
}
import "testing"
import "fmt"
import "github.com/rfwlab/rfw/v1/dom"
import "github.com/rfwlab/rfw/v1/events"
import "github.com/rfwlab/rfw/v1/js"
js