Interface HasItems<E extends Element, C extends HasItems<E,C,S>, S extends HasIdentifier<? extends HTMLElement, ?>>
- Type Parameters:
E- the element type of the main componentC- the type of the main componentS- the type of the subcomponent (representing an item)
- All Superinterfaces:
IsElement<E>, Iterable<S>, TypedBuilder<E,C>
- All Known Subinterfaces:
Ordered<E,C, S>
- All Known Implementing Classes:
Accordion, ActionList, ActionListGroup, AlertGroup, Breadcrumb, DataList, DescriptionList, ExpandableNavigationGroup, Finder, FinderColumn, Form, JumpLinks, JumpLinksList, LabelGroup, List, MenuList, Navigation, NavigationGroup, NotificationDrawerList, ProgressStepper, SimpleList, SimpleListGroup, Tabs, Tbody, ToggleGroup, ToolbarContent, ToolbarFilterContent, ToolbarGroup, ToolbarToggleGroup, Tr, TreeView, TreeViewItem, Wizard, WizardNav
public interface HasItems<E extends Element, C extends HasItems<E,C,S>, S extends HasIdentifier<? extends HTMLElement, ?>>
extends Iterable<S>, TypedBuilder<E,C>, IsElement<E>
The HasItems interface represents a component that can contain a collection of items. It provides methods for adding,
removing, and manipulating items within the component.
The child component must implement HasIdentifier and often also implements
ComponentContext.
record User(String id, String name, boolean valid) {}
// construction time
List<User> users = emptyList();
DataList dl = dataList()
.addItems(users, user -> dataListItem(user.id())
.store("user", user)
.addCell(dataListCell().icon(user()))
.addCell(dataListCell()
.add(span()
.id(user.id())
.text(user.name()))));
// later, responding to an event or something similar
for (DataListItem item : dl) {
User user = item.get("user");
item.toggle("invalid-user", !user.valid());
}
-
Method Summary
Modifier and TypeMethodDescriptionAdds an item to the component.default CAdds a single item to the component.default <T> CAdds a collection of items to the component.voidclear()Clears all items from the component.booleanChecks whether the component contains an item associated with the given identifier.booleanisEmpty()Checks whether the collection of items in the component is empty.Retrieves the item associated with the specified identifier from the component.items()Retrieves a list of all items contained in the component.onAdd(AddItemHandler<C, S> onAdd) Registers a callback to be invoked whenever a new item is added to the component.onRemove(RemoveItemHandler<C, S> onRemove) Registers a callback to be invoked whenever an item is removed from the component.onUpdate(UpdateItemHandler<C, S> onUpdate) Registers a callback to be invoked whenever an item is updated in the component.voidremoveItem(String identifier) Removes an item from the component based on the provided identifier.default voidreplaceItemElement(S item, BiConsumer<S, S> whenReplaced) Replaces an existing item in the component with a new item.intsize()Retrieves the total number of items currently contained in the component.default voidupdateItem(String identifier) Updates an item in the component identified by a given identifier.voidupdateItem(S item) Updates an existing item in the component.default <T> voidupdateItem(T item, Function<T, S> display) Updates an item in the component by applying a transformation function to the item and updating the resulting component.Methods inherited from interface Iterable
forEach, iterator, spliteratorMethods inherited from interface TypedBuilder
that
-
Method Details
-
addItems
Adds a collection of items to the component. The method applies the provided function to each item in the given iterable to transform it into a subcomponent, which is then added to the component.- Type Parameters:
T- the type of the items in the iterable- Parameters:
items- the iterable collection of items to be addeddisplay- the function used to transform each item into a subcomponent- Returns:
- the builder instance after adding the items
-
addItem
-
add
-
onAdd
Registers a callback to be invoked whenever a new item is added to the component.- Parameters:
onAdd- aAddItemHandlerthat takes the builder instance and the item being added as arguments- Returns:
- the builder instance after adding the callback
-
items
-
size
int size()Retrieves the total number of items currently contained in the component.- Returns:
- the number of items contained in the component
-
isEmpty
boolean isEmpty()Checks whether the collection of items in the component is empty.- Returns:
trueif the component contains no items;falseotherwise
-
contains
Checks whether the component contains an item associated with the given identifier.- Parameters:
identifier- the identifier of the item to be checked- Returns:
- true if the component contains an item associated with the provided identifier, false otherwise
-
item
-
updateItem
Updates an item in the component by applying a transformation function to the item and updating the resulting component.- Type Parameters:
T- the type of the input item- Parameters:
item- the item to be updateddisplay- the function used to transform the input item into a component
-
updateItem
Updates an item in the component identified by a given identifier. The item is obtained using the identifier and then updated in the component.- Parameters:
identifier- the identifier of the item to be updated
-
updateItem
Updates an existing item in the component.- Parameters:
item- the item to be updated
-
onUpdate
Registers a callback to be invoked whenever an item is updated in the component.- Parameters:
onUpdate- aUpdateItemHandlerthat takes the component, the previous state of the item, and the updated state of the item as arguments- Returns:
- the builder instance after adding the callback
-
replaceItemElement
Replaces an existing item in the component with a new item. If an item with the same identifier exists, its element is replaced with the element of the provided item, and a callback action is executed after the update.This method can be used when implementing the
updateItem(String)method. It is not meant to be used directly!- Parameters:
item- the new item to replace the existing itemwhenReplaced- a callback action to be executed after the item has been updated; it receives the old item and the new item as arguments
-
removeItem
Removes an item from the component based on the provided identifier.- Parameters:
identifier- the identifier of the item to be removed
-
onRemove
Registers a callback to be invoked whenever an item is removed from the component.- Parameters:
onRemove- aRemoveItemHandlerthat takes the component and the item being removed as arguments- Returns:
- the builder instance after adding the callback
-
clear
void clear()Clears all items from the component. This method removes any existing items within the component, leaving it empty. Any associated or registered callbacks related to the removal of items may be invoked as part of this operation.
-