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
textstring|tableThe item to add (string or item table)
Returns
CollectionselfThe Collection instance
Usage
lua
Collection:addItem("New Item")lua
Collection:addItem({text="Item", callback=function() end})Collection:removeItem(index)
Removes an item from the Collection
Parameters
indexnumberThe index of the item to remove
Returns
CollectionselfThe Collection instance
Usage
lua
Collection:removeItem(1)Collection:clear()
Clears all items from the Collection
Returns
CollectionselfThe Collection instance
Usage
lua
Collection:clear()Collection:getSelectedItems()
Gets the currently selected items
Returns
tableselectedCollection of selected items
Usage
lua
local selected = Collection:getSelectedItems()Collection:getSelectedItem()
Gets first selected item
Returns
selectedThefirst item
Collection:getSelectedIndex()
Gets the index of the first selected item
Returns
indexTheindex of the first selected item, or nil if none selected
Usage
lua
local index = Collection:getSelectedIndex()Collection:selectNext()
Selects the next item in the collection
Returns
CollectionselfThe Collection instance
Collection:selectPrevious()
Selects the previous item in the collection
Returns
CollectionselfThe Collection instance
Collection:onSelect(callback)
Registers a callback for the select event
Parameters
callbackfunctionThe callback function to register
Returns
CollectionselfThe Collection instance
Usage
lua
Collection:onSelect(function(index, item) print("Selected item:", index, item) end)