Files
Basalt2/docs/references/elements/Collection.md
2025-10-29 16:56:08 +00:00

3.3 KiB

Collection

This is the Collection class. It provides a collection of items

Extends: VisualElement

Properties

Property Type Default Description
selectable boolean true Whether items can be selected
multiSelection boolean false Whether multiple items can be selected at once
selectedBackground color blue Background color for selected items
selectedForeground color white Text color for selected items

Events

Event Parameters Description
onSelect index number, item table Fired when an item is selected

Functions

Method Returns Description
Collection:addItem Collection Adds an item to the Collection
Collection:removeItem Collection Removes an item from the Collection
Collection:clear Collection Clears all items from the Collection
Collection:getSelectedItems table Gets the currently selected items
Collection:getSelectedItem selected Gets first selected item
Collection:getSelectedIndex index Gets the index of the first selected item
Collection:selectNext Collection Selects the next item
Collection:selectPrevious Collection Selects the previous item
Collection:onSelect Collection Registers a callback for the select event

Collection:addItem(text)

Adds an item to the Collection

Parameters

  • text string|table The item to add (string or item table)

Returns

  • Collection self The Collection instance

Usage

Collection:addItem("New Item")
Collection:addItem({text="Item", callback=function() end})

Collection:removeItem(index)

Removes an item from the Collection

Parameters

  • index number The index of the item to remove

Returns

  • Collection self The Collection instance

Usage

Collection:removeItem(1)

Collection:clear()

Clears all items from the Collection

Returns

  • Collection self The Collection instance

Usage

Collection:clear()

Collection:getSelectedItems()

Gets the currently selected items

Returns

  • table selected Collection of selected items

Usage

local selected = Collection:getSelectedItems()

Collection:getSelectedItem()

Gets first selected item

Returns

  • selected The first item

Collection:getSelectedIndex()

Gets the index of the first selected item

Returns

  • index The index of the first selected item, or nil if none selected

Usage

local index = Collection:getSelectedIndex()

Collection:selectNext()

Selects the next item in the collection

Returns

  • Collection self The Collection instance

Collection:selectPrevious()

Selects the previous item in the collection

Returns

  • Collection self The Collection instance

Collection:onSelect(callback)

Registers a callback for the select event

Parameters

  • callback function The callback function to register

Returns

  • Collection self The Collection instance

Usage

Collection:onSelect(function(index, item) print("Selected item:", index, item) end)