virtual
API
virtual
packageAPI reference for the virtual
package.
Imports
(6)
S
struct
VirtualList
VirtualList renders only the portion of a list that is visible within its container.
dom/virtual/list.go:15-21
type VirtualList struct
Methods
update
Method
update recalculates the visible range and mounts the required items.
func (*VirtualList) update()
{
if v.Container.IsNull() || v.Container.IsUndefined() {
return
}
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
Method
Destroy removes scroll listeners and cleans up resources.
func (*VirtualList) Destroy()
{
if v.stopScroll != nil {
v.stopScroll()
}
}
Fields
| Name | Type | Description |
|---|---|---|
| Container | dom.Element | |
| Total | int | |
| ItemHeight | int | |
| Render | func(i int) string | |
| stopScroll | func() |
T
type
List
List is the concise name for VirtualList.
dom/virtual/list.go:24-24
type List VirtualList
F
function
NewVirtualList
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
dom/virtual/list.go:27-42
func NewVirtualList(containerID string, total, itemHeight int, render func(i int) string) *VirtualList
{
v := &VirtualList{
Container: dom.Doc().ByID(containerID),
Total: total,
ItemHeight: itemHeight,
Render: render,
}
if v.Container.IsNull() || v.Container.IsUndefined() {
return v
}
v.stopScroll = events.OnScroll(v.Container.Value, func(js.Value) {
v.update()
})
v.update()
return v
}
S
struct
VirtualList
VirtualList is a placeholder that does nothing on non-JS/WASM platforms.
dom/virtual/list_stub.go:7-7
type VirtualList struct
Methods
update
Method
update recalculates the visible range and mounts the required items.
func (*VirtualList) update()
{
if v.Container.IsNull() || v.Container.IsUndefined() {
return
}
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
Method
Destroy removes scroll listeners and cleans up resources.
func (*VirtualList) Destroy()
{
if v.stopScroll != nil {
v.stopScroll()
}
}
T
type
List
List is the concise name for VirtualList.
dom/virtual/list_stub.go:10-10
type List VirtualList
F
function
NewVirtualList
NewVirtualList returns an empty VirtualList placeholder.
Parameters
_
string
_
int
_
int
_
func(int) string
Returns
dom/virtual/list_stub.go:13-15
func NewVirtualList(_ string, _ int, _ int, _ func(int) string) *VirtualList
{
return &VirtualList{}
}
F
function
TestVirtualListRendersVisibleItems
Parameters
t
dom/virtual/virtual_test.go:12-29
func TestVirtualListRendersVisibleItems(t *testing.T)
{
c := dom.CreateElement("div")
c.Set("id", "vlist")
c.Get("style").Set("height", "60px")
c.Get("style").Set("overflow", "auto")
dom.Doc().Body().AppendChild(c)
defer c.Call("remove")
v := NewVirtualList("vlist", 100, 20, func(int) string {
return "<div class='it'>row</div>"
})
defer v.Destroy()
html := c.HTML()
if !strings.Contains(html, "class='it'") && !strings.Contains(html, `class="it"`) {
t.Fatalf("expected rendered items, got %q", html)
}
}
F
function
TestVirtualListMissingContainer
Parameters
t
dom/virtual/virtual_test.go:31-37
func TestVirtualListMissingContainer(t *testing.T)
{
v := NewVirtualList("does-not-exist", 10, 20, func(int) string { return "" })
if v == nil {
t.Fatalf("expected List instance")
}
v.Destroy()
}