Class Crud<E>

java.lang.Object
com.vaadin.flow.component.Component
com.vaadin.flow.component.crud.Crud<E>
Type Parameters:
E - the bean type
All Implemented Interfaces:
com.vaadin.flow.component.AttachNotifier, com.vaadin.flow.component.DetachNotifier, com.vaadin.flow.component.HasElement, com.vaadin.flow.component.HasSize, com.vaadin.flow.component.HasStyle, com.vaadin.flow.component.HasTheme, Serializable

@Tag("vaadin-crud") @NpmPackage(value="@vaadin/polymer-legacy-adapter",version="24.10.0-alpha2") @NpmPackage(value="@vaadin/crud",version="24.10.0-alpha2") @JsModule("@vaadin/polymer-legacy-adapter/style-modules.js") @JsModule("@vaadin/crud/src/vaadin-crud.js") @JsModule("@vaadin/crud/src/vaadin-crud-edit-column.js") public class Crud<E> extends com.vaadin.flow.component.Component implements com.vaadin.flow.component.HasSize, com.vaadin.flow.component.HasTheme, com.vaadin.flow.component.HasStyle
A component for performing CRUD operations on a data backend (e.g entities from a database).
Author:
Vaadin Ltd
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    Event fired when the user cancels the creation of a new item or modifications to an existing item.
    static class 
    Event fired when the user tries to delete an existing item.
    static class 
    Event fired when the user starts to edit an existing item.
    static enum 
    Determines whether an item presented for editing is to be treated as a new item or an existing item.
    static class 
    Event fired when the user starts to create a new item.
    static class 
    Event fired when the user tries to save a new item or modifications to an existing item.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Instantiates a new Crud with no grid, editor and runtime bean type information.
    Crud(Class<E> beanType, CrudEditor<E> editor)
    Instantiates a new Crud for the given bean type and uses the supplied editor.
    Crud(Class<E> beanType, Grid<E> grid, CrudEditor<E> editor)
    Instantiates a new Crud using a custom grid.
  • Method Summary

    Modifier and Type
    Method
    Description
    com.vaadin.flow.shared.Registration
    addCancelListener(com.vaadin.flow.component.ComponentEventListener<Crud.CancelEvent<E>> listener)
    Registers a listener to be notified when the user cancels a new item creation or existing item modification in progress.
    com.vaadin.flow.shared.Registration
    addDeleteListener(com.vaadin.flow.component.ComponentEventListener<Crud.DeleteEvent<E>> listener)
    Registers a listener to be notified when the user tries to delete an existing item.
    static void
    A helper method to add an edit column to a grid.
    static void
    addEditColumn(Grid<?> grid, CrudI18n crudI18n)
    A helper method to add an edit column to a grid.
    com.vaadin.flow.shared.Registration
    addEditListener(com.vaadin.flow.component.ComponentEventListener<Crud.EditEvent<E>> listener)
    Registers a listener to be notified when the user starts to edit an existing item.
    com.vaadin.flow.shared.Registration
    addNewListener(com.vaadin.flow.component.ComponentEventListener<Crud.NewEvent<E>> listener)
    Registers a listener to be notified when the user starts to create a new item.
    com.vaadin.flow.shared.Registration
    addSaveListener(com.vaadin.flow.component.ComponentEventListener<Crud.SaveEvent<E>> listener)
    Registers a listener to be notified when the user tries to save a new item or modifications to an existing item.
    void
    Adds theme variants to the component.
    void
    edit(E item, Crud.EditMode editMode)
    Initiates an item edit from the server-side.
    Gets the runtime bean type information
    Gets the Crud cancel button
    com.vaadin.flow.data.provider.DataProvider<E,?>
    Gets the data provider supplying the grid data.
    Gets the Crud editor delete button
    Gets the crud editor.
    Gets the current editor position on the desktop screen.
    Gets the grid
    com.vaadin.flow.component.Component
    Gets the Crud new item button
    Gets the Crud save button
    boolean
    Gets visiblity state of toolbar
    static boolean
    Checks if an edit column has been added to the Grid using Crud.addEditColumn(Grid)
    boolean
    Gets whether click on row to edit item is enabled or not.
    protected void
    onAttach(com.vaadin.flow.component.AttachEvent attachEvent)
     
    static void
    Removes the crud edit column from a grid
    void
    Removes theme variants from the component.
    void
    setBeanType(Class<E> beanType)
    Sets the runtime bean type information.
    void
    setDataProvider(com.vaadin.flow.data.provider.DataProvider<E,?> provider)
    Sets the data provider for the grid.
    void
    setDirty(boolean dirty)
    Set the dirty state of the Crud.
    void
    setEditOnClick(boolean editOnClick)
    Sets the option to open item to edit by row click.
    void
    Sets the editor.
    void
    Sets how editor will be presented on desktop screen.
    void
    setGrid(Grid<E> grid)
    Sets the grid
    void
    Sets the internationalized messages to be used by this crud instance.
    void
    setNewButton(com.vaadin.flow.component.Component button)
    Sets the Crud new item button
    void
    setOpened(boolean opened)
    Opens or closes the editor.
    void
    setToolbar(com.vaadin.flow.component.Component... components)
    Sets the content of the toolbar.
    void
    setToolbarVisible(boolean value)
    Controls visiblity of toolbar

    Methods 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, onDetach, onEnabledStateChanged, removeFromParent, scrollIntoView, scrollIntoView, set, setElement, setId, setVisible

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface com.vaadin.flow.component.AttachNotifier

    addAttachListener

    Methods inherited from interface com.vaadin.flow.component.DetachNotifier

    addDetachListener

    Methods inherited from interface com.vaadin.flow.component.HasElement

    getElement

    Methods 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, setWidthFull

    Methods inherited from interface com.vaadin.flow.component.HasStyle

    addClassName, addClassNames, getClassName, getClassNames, getStyle, hasClassName, removeClassName, removeClassNames, setClassName, setClassName

    Methods inherited from interface com.vaadin.flow.component.HasTheme

    addThemeName, addThemeNames, getThemeName, getThemeNames, hasThemeName, removeThemeName, removeThemeNames, setThemeName, setThemeName
  • Constructor Details

    • Crud

      public Crud(Class<E> beanType, Grid<E> grid, CrudEditor<E> editor)
      Instantiates a new Crud using a custom grid.
      Parameters:
      beanType - the class of items
      grid - the grid with which the items listing should be displayed
      editor - the editor for manipulating individual items
      See Also:
    • Crud

      public Crud(Class<E> beanType, CrudEditor<E> editor)
      Instantiates a new Crud for the given bean type and uses the supplied editor. Furthermore, it displays the items using the built-in grid.
      Parameters:
      beanType - the class of items
      editor - the editor for manipulating individual items
      See Also:
    • Crud

      public Crud()
      Instantiates a new Crud with no grid, editor and runtime bean type information. The editor and bean type must be initialized before a Crud is put into full use therefore this constructor only exists for partial initialization in order to support template binding.
      See Also:
  • Method Details

    • onAttach

      protected void onAttach(com.vaadin.flow.component.AttachEvent attachEvent)
      Overrides:
      onAttach in class com.vaadin.flow.component.Component
    • edit

      public void edit(E item, Crud.EditMode editMode)
      Initiates an item edit from the server-side. This sets the supplied item as the working bean and opens the edit dialog.
      Parameters:
      item - the item to be edited
      editMode - the edit mode
    • setOpened

      public void setOpened(boolean opened)
      Opens or closes the editor. In most use cases opening or closing the editor is automatically done by the component and this method does not need to be called.
      Parameters:
      opened - true to open or false to close
    • setDirty

      public void setDirty(boolean dirty)
      Set the dirty state of the Crud.

      A dirty Crud has its editor Save button enabled. Ideally a Crud automatically detects if it is dirty based on interactions with the form fields within it but in some special cases (e.g with composites) this might not be automatically detected. For such cases this method could be used to explicitly set the dirty state of the Crud editor.

      NOTE: editor Save button will not be automatically enabled in case its enabled state was changed with getSaveButton()

      Parameters:
      dirty - true if dirty and false if otherwise.
      See Also:
    • getBeanType

      public Class<E> getBeanType()
      Gets the runtime bean type information
      Returns:
      the bean type
    • setBeanType

      public void setBeanType(Class<E> beanType)
      Sets the runtime bean type information. If no grid exists a built-in grid is created since the bean type information is now known. When injecting a Crud with @Id this method must be called before the crud is put into use.
      Parameters:
      beanType - the bean type
    • getGrid

      public Grid<E> getGrid()
      Gets the grid
      Returns:
      the grid
    • setGrid

      public void setGrid(Grid<E> grid)
      Sets the grid
      Parameters:
      grid - the grid
    • getEditor

      public CrudEditor<E> getEditor()
      Gets the crud editor.
      Returns:
      the crud editor
    • setEditor

      public void setEditor(CrudEditor<E> editor)
      Sets the editor. When injecting a Crud with @Id this method must be called before the crud is put into use.
      Parameters:
      editor - the editor
    • setEditorPosition

      public void setEditorPosition(CrudEditorPosition editorPosition)
      Sets how editor will be presented on desktop screen.

      The default position is CrudEditorPosition.OVERLAY.

      Parameters:
      editorPosition - the editor position, never null
      See Also:
    • getEditorPosition

      public CrudEditorPosition getEditorPosition()
      Gets the current editor position on the desktop screen.

      The default position is CrudEditorPosition.OVERLAY.

      Returns:
      the editor position
    • setEditOnClick

      public void setEditOnClick(boolean editOnClick)
      Sets the option to open item to edit by row click.

      If enabled, it removes the edit column created by CrudGrid.

      Parameters:
      editOnClick - true to enable it (false, by default).
    • isEditOnClick

      public boolean isEditOnClick()
      Gets whether click on row to edit item is enabled or not.
      Returns:
      true if enabled, false otherwise
    • setToolbar

      public void setToolbar(com.vaadin.flow.component.Component... components)
      Sets the content of the toolbar.
      Parameters:
      components - the content to be set
    • setI18n

      public void setI18n(CrudI18n i18n)
      Sets the internationalized messages to be used by this crud instance.
      Parameters:
      i18n - the internationalized messages
      See Also:
    • setToolbarVisible

      public void setToolbarVisible(boolean value)
      Controls visiblity of toolbar
      Parameters:
      value -
    • getToolbarVisible

      public boolean getToolbarVisible()
      Gets visiblity state of toolbar
      Returns:
      true if toolbar is visible false otherwise
    • getNewButton

      public com.vaadin.flow.component.Component getNewButton()
      Gets the Crud new item button
      Returns:
      the new item button
    • setNewButton

      public void setNewButton(com.vaadin.flow.component.Component button)
      Sets the Crud new item button
      Parameters:
      button -
    • getDeleteButton

      public Button getDeleteButton()
      Gets the Crud editor delete button
      Returns:
      the delete button
    • getSaveButton

      public Button getSaveButton()
      Gets the Crud save button

      NOTE: State of the button set with HasEnabled.setEnabled(boolean) will remain even if dirty state of the crud changes

      Returns:
      the save button
      See Also:
    • getCancelButton

      public Button getCancelButton()
      Gets the Crud cancel button
      Returns:
      the cancel button
    • addThemeVariants

      public void addThemeVariants(CrudVariant... variants)
      Adds theme variants to the component.
      Parameters:
      variants - theme variants to add
    • removeThemeVariants

      public void removeThemeVariants(CrudVariant... variants)
      Removes theme variants from the component.
      Parameters:
      variants - theme variants to remove
    • addNewListener

      public com.vaadin.flow.shared.Registration addNewListener(com.vaadin.flow.component.ComponentEventListener<Crud.NewEvent<E>> listener)
      Registers a listener to be notified when the user starts to create a new item.
      Parameters:
      listener - a listener to be notified
      Returns:
      a handle that can be used to unregister the listener
    • addEditListener

      public com.vaadin.flow.shared.Registration addEditListener(com.vaadin.flow.component.ComponentEventListener<Crud.EditEvent<E>> listener)
      Registers a listener to be notified when the user starts to edit an existing item.
      Parameters:
      listener - a listener to be notified
      Returns:
      a handle that can be used to unregister the listener
    • addSaveListener

      public com.vaadin.flow.shared.Registration addSaveListener(com.vaadin.flow.component.ComponentEventListener<Crud.SaveEvent<E>> listener)
      Registers a listener to be notified when the user tries to save a new item or modifications to an existing item.
      Parameters:
      listener - a listener to be notified
      Returns:
      a handle that can be used to unregister the listener
    • addCancelListener

      public com.vaadin.flow.shared.Registration addCancelListener(com.vaadin.flow.component.ComponentEventListener<Crud.CancelEvent<E>> listener)
      Registers a listener to be notified when the user cancels a new item creation or existing item modification in progress.
      Parameters:
      listener - a listener to be notified
      Returns:
      a handle that can be used to unregister the listener
    • addDeleteListener

      public com.vaadin.flow.shared.Registration addDeleteListener(com.vaadin.flow.component.ComponentEventListener<Crud.DeleteEvent<E>> listener)
      Registers a listener to be notified when the user tries to delete an existing item.
      Parameters:
      listener - a listener to be notified
      Returns:
      a handle that can be used to unregister the listener
    • getDataProvider

      public com.vaadin.flow.data.provider.DataProvider<E,?> getDataProvider()
      Gets the data provider supplying the grid data.
      Returns:
      the data provider for the grid
    • setDataProvider

      public void setDataProvider(com.vaadin.flow.data.provider.DataProvider<E,?> provider)
      Sets the data provider for the grid.
      Parameters:
      provider - the data provider for the grid
    • addEditColumn

      public static void addEditColumn(Grid<?> grid)
      A helper method to add an edit column to a grid. Clicking on the edit cell for a row opens the item for editing in the editor.
      Parameters:
      grid - the grid in which to add the edit column
      See Also:
    • addEditColumn

      public static void addEditColumn(Grid<?> grid, CrudI18n crudI18n)
      A helper method to add an edit column to a grid. Clicking on the edit cell for a row opens the item for editing in the editor. Additionally, the i18n object is used for setting the aria-label for the button, improving accessibility.
      Parameters:
      grid - the grid in which to add the edit column
      crudI18n - the i18n object for localizing the accessibility of the edit column
    • removeEditColumn

      public static void removeEditColumn(Grid<?> grid)
      Removes the crud edit column from a grid
      Parameters:
      grid - the grid from which to remove the edit column
      See Also:
    • hasEditColumn

      public static boolean hasEditColumn(Grid<?> grid)
      Checks if an edit column has been added to the Grid using Crud.addEditColumn(Grid)
      Parameters:
      grid - the grid to check
      Returns:
      true if an edit column is present or false if otherwise
      See Also: