Package com.storedobject.ui
Class CardGrid<T>
java.lang.Object
com.vaadin.flow.component.Component
com.vaadin.flow.component.HtmlComponent
com.vaadin.flow.component.HtmlContainer
com.vaadin.flow.component.html.Div
com.storedobject.ui.CardGrid<T>
- Type Parameters:
T- The type of object associated with the cards in the grid.
- All Implemented Interfaces:
com.vaadin.flow.component.AttachNotifier,com.vaadin.flow.component.ClickNotifier<com.vaadin.flow.component.html.Div>,com.vaadin.flow.component.DetachNotifier,com.vaadin.flow.component.HasComponents,com.vaadin.flow.component.HasElement,com.vaadin.flow.component.HasEnabled,com.vaadin.flow.component.HasOrderedComponents,com.vaadin.flow.component.HasSize,com.vaadin.flow.component.HasStyle,com.vaadin.flow.component.HasText,Serializable
- Direct Known Subclasses:
ObjectCardGrid
public class CardGrid<T>
extends com.vaadin.flow.component.html.Div
A CardGrid is a UI component designed to display a grid of cards.
The grid is dynamic and adjusts to the available space while maintaining
a consistent card width and gap between the cards.
CardGrid extends the
Div component and provides methods for customizing
the appearance and behavior of the grid layout. It supports setting a custom card width
and gap size, and it can also be linked to a CardDashboard for managing its content.- Author:
- Syam
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface com.vaadin.flow.component.HasText
com.vaadin.flow.component.HasText.WhiteSpace -
Constructor Summary
ConstructorsConstructorDescriptionCardGrid()Constructs a new CardGrid instance and initializes its default styling and layout. -
Method Summary
Modifier and TypeMethodDescriptioncom.vaadin.flow.shared.RegistrationaddCardSelectedListener(Consumer<Card<T>> listener) Registers a listener triggered whenever a card is selected in the grid.getCards()Retrieves a stream of allCardelements currently present as children of thisCardGrid.intReturns the width of the cards in the grid.Returns the associatedCardDashboardinstance for thisCardGrid.intgetGap()Retrieves the size of the gap between cards in the grid layout.Retrieves the currently selected card based on the grid's current selection mode.Retrieves a stream of all cards that are currently selected.final com.vaadin.flow.component.grid.Grid.SelectionModeRetrieves the current selection mode of the grid.voidDisables card selection in the grid.booleanChecks whether card selection is currently being ignored in the grid.voidselectAllCards(boolean select) Toggles the selection state of all cards in the grid based on the specified parameter.voidsetCardWidth(int cardWidth) Sets the width of the cards displayed in the grid.voidsetGap(int gap) Sets the gap size between cards in the grid layout.voidsetSelectionMode(com.vaadin.flow.component.grid.Grid.SelectionMode selectionMode) Sets the selection mode for the grid.Methods inherited from class com.vaadin.flow.component.HtmlComponent
getTitle, setTitleMethods inherited from class com.vaadin.flow.component.Component
addListener, findAncestor, fireEvent, from, get, getChildren, getElement, getEventBus, getId, getListeners, getLocale, getParent, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getUI, hasListener, isAttached, isTemplateMapped, isVisible, onAttach, onDetach, onEnabledStateChanged, removeFromParent, scrollIntoView, scrollIntoView, set, setElement, setId, setVisibleMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.vaadin.flow.component.AttachNotifier
addAttachListenerMethods inherited from interface com.vaadin.flow.component.ClickNotifier
addClickListener, addClickShortcut, addDoubleClickListener, addSingleClickListenerMethods inherited from interface com.vaadin.flow.component.DetachNotifier
addDetachListenerMethods inherited from interface com.vaadin.flow.component.HasComponents
add, add, add, addComponentAsFirst, addComponentAtIndex, getChildren, getComponentAt, getComponentCount, indexOf, remove, remove, removeAll, replaceMethods inherited from interface com.vaadin.flow.component.HasElement
getElementMethods inherited from interface com.vaadin.flow.component.HasEnabled
isEnabled, setEnabledMethods inherited from interface com.vaadin.flow.component.HasSize
getHeight, getHeightUnit, getMaxHeight, getMaxWidth, getMinHeight, getMinWidth, getWidth, getWidthUnit, setHeight, setHeight, setHeightFull, setMaxHeight, setMaxHeight, setMaxWidth, setMaxWidth, setMinHeight, setMinHeight, setMinWidth, setMinWidth, setSizeFull, setSizeUndefined, setWidth, setWidth, setWidthFullMethods inherited from interface com.vaadin.flow.component.HasStyle
addClassName, addClassNames, getClassName, getClassNames, getStyle, hasClassName, removeClassName, removeClassNames, setClassName, setClassNameMethods inherited from interface com.vaadin.flow.component.HasText
getText, getWhiteSpace, setText, setWhiteSpace
-
Constructor Details
-
CardGrid
public CardGrid()Constructs a new CardGrid instance and initializes its default styling and layout. The CardGrid uses a CSS grid layout with specific configurations:- display is set to "grid". - grid rows have a dynamic height with a minimum of 50px. - content alignment is set to stretch horizontally and start vertically. - the grid width occupies 100% of the container.
Additionally, the default gap size and card width are set:- The initial gap size between grid items is 16 pixels. - The initial card width for grid items is 350 pixels.
-
-
Method Details
-
setGap
public void setGap(int gap) Sets the gap size between cards in the grid layout. The gap size defines the spacing in pixels and will be clamped to a minimum value of 0 to ensure non-negative spacing.- Parameters:
gap- the gap size in pixels to be set between cards
-
getGap
public int getGap()Retrieves the size of the gap between cards in the grid layout. The gap determines the spacing between individual cards.- Returns:
- the current gap size in pixels
-
setCardWidth
public void setCardWidth(int cardWidth) Sets the width of the cards displayed in the grid. If the specified width is less than 1, the method defaults the card width to 350 pixels. The card width is used to dynamically configure the grid layout so that the cards fit within the available space while maintaining the specified width.- Parameters:
cardWidth- the width to be set for the cards, in pixels. A value less than 1 will reset the card width to 350 pixels.
-
getCardWidth
public int getCardWidth()Returns the width of the cards in the grid.- Returns:
- the card width in pixels
-
getDashboard
Returns the associatedCardDashboardinstance for thisCardGrid. TheCardDashboardmay contain additional logic, behavior, or state associated with the grid's content and layout.- Returns:
- the
CardDashboardlinked to thisCardGrid, ornullif no dashboard is currently set.
-
setSelectionMode
public void setSelectionMode(com.vaadin.flow.component.grid.Grid.SelectionMode selectionMode) Sets the selection mode for the grid. The selection mode determines how cards in the grid can be selected, such as single, multi, or none.- Parameters:
selectionMode- theGrid.SelectionModeto define the card selection behavior in the grid. This value cannot be null.
-
getSelectionMode
public final com.vaadin.flow.component.grid.Grid.SelectionMode getSelectionMode()Retrieves the current selection mode of the grid. The selection mode defines how cards in the grid can be selected (e.g., single, multiple, or none).- Returns:
- the current
Grid.SelectionModefor the grid
-
addCardSelectedListener
Registers a listener triggered whenever a card is selected in the grid. The listener will receive the selectedCardas its input parameter.- Parameters:
listener- aConsumerthat processes the selectedCard. This listener will be notified whenever a card is selected.- Returns:
- a
Registrationinstance that can be used to remove the listener when it is no longer necessary.
-
selectAllCards
public void selectAllCards(boolean select) Toggles the selection state of all cards in the grid based on the specified parameter. This method only operates when the grid's selection mode is set toGrid.SelectionMode.MULTI.- Parameters:
select- a boolean value indicating the desired selection state for all cards. If true, all cards will be marked as selected; otherwise, all cards will be deselected.
-
getSelectedCard
Retrieves the currently selected card based on the grid's current selection mode. If the selection mode is set toNONE, no card can be selected and this method will returnnull. If the selection mode isSINGLE, this method will return the selected card if one is selected, ornullif no card is selected. If multiple cards are selected, the first card in the stream of selected cards will be returned.- Returns:
- the selected
Cardif a selection exists;nullotherwise
-
getSelectedCards
-
getCards
-
ignoreSelection
public void ignoreSelection()Disables card selection in the grid. When this method is invoked, it prevents the grid from processing any card selection events, effectively ignoring selection input or state changes. This can be useful in scenarios where card selection functionality needs to be temporarily suspended without altering the grid's selection state. Use case: When you have clickable components inside the cards, you may not want to fire the selection events when the user clicks on them. In such cases, you can temporarily suspend card selection to avoid unintended behavior. This is typically achieved by invoking this method from within your click-handlers. It will be automatically enabled again when your click-handlers complete their execution. -
isIgnoreSelection
public boolean isIgnoreSelection()Checks whether card selection is currently being ignored in the grid. When this method returnstrue, card selection events are not processed, effectively disabling selection functionality for the grid.- Returns:
trueif card selection is being ignored;falseotherwise
-