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 component
C - the type of the main component
S - 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 Type
    Method
    Description
    add(S item)
    Adds an item to the component.
    default C
    addItem(S item)
    Adds a single item to the component.
    default <T> C
    addItems(Iterable<T> items, Function<T,S> display)
    Adds a collection of items to the component.
    void
    Clears all items from the component.
    boolean
    contains(String identifier)
    Checks whether the component contains an item associated with the given identifier.
    boolean
    Checks whether the collection of items in the component is empty.
    item(String identifier)
    Retrieves the item associated with the specified identifier from the component.
    default List<S>
    Retrieves a list of all items contained in the component.
    Registers a callback to be invoked whenever a new item is added to the component.
    Registers a callback to be invoked whenever an item is removed from the component.
    Registers a callback to be invoked whenever an item is updated in the component.
    void
    removeItem(String identifier)
    Removes an item from the component based on the provided identifier.
    default void
    replaceItemElement(S item, BiConsumer<S,S> whenReplaced)
    Replaces an existing item in the component with a new item.
    int
    Retrieves the total number of items currently contained in the component.
    default void
    updateItem(String identifier)
    Updates an item in the component identified by a given identifier.
    void
    updateItem(S item)
    Updates an existing item in the component.
    default <T> void
    updateItem(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 IsElement

    element

    Methods inherited from interface Iterable

    forEach, iterator, spliterator

    Methods inherited from interface TypedBuilder

    that
  • Method Details

    • addItems

      default <T> C addItems(Iterable<T> items, Function<T,S> display)
      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 added
      display - the function used to transform each item into a subcomponent
      Returns:
      the builder instance after adding the items
    • addItem

      default C addItem(S item)
      Adds a single item to the component.
      Parameters:
      item - the item to be added
      Returns:
      the builder instance after adding the item
    • add

      C add(S item)
      Adds an item to the component.
      Parameters:
      item - the item to be added to the component
      Returns:
      the builder instance after the item has been added
    • onAdd

      C onAdd(AddItemHandler<C,S> onAdd)
      Registers a callback to be invoked whenever a new item is added to the component.
      Parameters:
      onAdd - a AddItemHandler that takes the builder instance and the item being added as arguments
      Returns:
      the builder instance after adding the callback
    • items

      default List<S> items()
      Retrieves a list of all items contained in the component.
      Returns:
      a list of items of type S contained in the component
    • 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:
      true if the component contains no items; false otherwise
    • contains

      boolean contains(String identifier)
      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

      S item(String identifier)
      Retrieves the item associated with the specified identifier from the component.
      Parameters:
      identifier - the identifier of the item to be retrieved
      Returns:
      the item associated with the given identifier, or null if no item is found
    • updateItem

      default <T> void updateItem(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.
      Type Parameters:
      T - the type of the input item
      Parameters:
      item - the item to be updated
      display - the function used to transform the input item into a component
    • updateItem

      default void updateItem(String identifier)
      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

      void updateItem(S item)
      Updates an existing item in the component.
      Parameters:
      item - the item to be updated
    • onUpdate

      C onUpdate(UpdateItemHandler<C,S> onUpdate)
      Registers a callback to be invoked whenever an item is updated in the component.
      Parameters:
      onUpdate - a UpdateItemHandler that 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

      default void replaceItemElement(S item, BiConsumer<S,S> whenReplaced)
      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 item
      whenReplaced - a callback action to be executed after the item has been updated; it receives the old item and the new item as arguments
    • removeItem

      void removeItem(String identifier)
      Removes an item from the component based on the provided identifier.
      Parameters:
      identifier - the identifier of the item to be removed
    • onRemove

      C onRemove(RemoveItemHandler<C,S> onRemove)
      Registers a callback to be invoked whenever an item is removed from the component.
      Parameters:
      onRemove - a RemoveItemHandler that 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.