VirtualList struct

VirtualList is a placeholder that does nothing on non-JS/WASM platforms.

Methods:

Destroy

Destroy performs no action in non-JS/WASM builds.


Show/Hide Method Body
{}

update

update recalculates the visible range and mounts the required items.


Show/Hide Method Body
{
	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

Destroy removes scroll listeners and cleans up resources.


Show/Hide Method Body
{
	if v.stopScroll != nil {
		v.stopScroll()
	}
}

NewVirtualList function

NewVirtualList returns an empty VirtualList placeholder.

Parameters:

  • _ string
  • _ int
  • _ int
  • _ func(int) string

Returns:

  • *VirtualList
Show/Hide Function Body
{
	return &VirtualList{}
}

TestVirtualListStub function

TestVirtualListStub ensures the stub implementation is non-nil and callable.

Parameters:

  • t *testing.T
Show/Hide Function Body
{
	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 struct

VirtualList renders only the portion of a list that is visible within its container.

Fields:

  • Container (dom.Element)
  • Total (int)
  • ItemHeight (int)
  • Render (func(i int) string)
  • stopScroll (func())

Methods:

Destroy

Destroy performs no action in non-JS/WASM builds.


Show/Hide Method Body
{}

update

update recalculates the visible range and mounts the required items.


Show/Hide Method Body
{
	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

Destroy removes scroll listeners and cleans up resources.


Show/Hide Method Body
{
	if v.stopScroll != nil {
		v.stopScroll()
	}
}

NewVirtualList function

NewVirtualList attaches a virtualized list to the element with the given id.

Parameters:

  • containerID string
  • total int
  • itemHeight int
  • render func(i int) string

Returns:

  • *VirtualList
Show/Hide Function Body
{
	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
}

testing import

Import example:

import "testing"

fmt import

Import example:

import "fmt"

github.com/rfwlab/rfw/v1/dom import

Import example:

import "github.com/rfwlab/rfw/v1/dom"

github.com/rfwlab/rfw/v1/events import

Import example:

import "github.com/rfwlab/rfw/v1/events"

github.com/rfwlab/rfw/v1/js import

Import example:

import "github.com/rfwlab/rfw/v1/js"

Imported as:

js