Docs

CRUD

CRUD is a component for managing a dataset. It allows for easy displaying, editing, creating, and deleting of items.
Note
Commercial feature

A commercial Vaadin subscription is required to use CRUD in your project.

CRUD is a component for managing a dataset. It allows for easy displaying, editing, creating, and deleting of items.

Open in a
new tab
<Crud include="firstName, lastName, email, profession" items={items} />

Columns

CRUD automatically generates columns for each field in the provided dataset. You can add columns with it and configure or remove existing ones.

Open in a
new tab
{/* Use 'include' or 'exclude' to select which fields to show */}
<Crud
  items={items}
  exclude="lastName, address, id, subscribe, membership, pictureUrl, manager"
/>

Editor

Data is edited using CRUD’s editor UI. By default, the editor is opened by clicking the edit Button in the last column. However, this button column can be removed if you want to provide an alternative way to engage the editor. For example, you set it to open using a double-click like so:

Open in a
new tab
<Crud
  include="firstName, lastName, email, profession"
  items={items}
  editedItem={editedItem}
  onEditedItemChanged={onEditedItemChanged}
>
  <Grid slot="grid" onDoubleClick={onDblClick}>
    <GridColumn path="firstName" header="First name" />
    <GridColumn path="lastName" header="Last name" />
    <GridColumn path="email" header="Email" />
    <GridColumn path="profession" header="Profession" />
  </Grid>
</Crud>

Editor Position

The editor can be positioned in an overlay, on the side or at the bottom.

Overlay

The overlay position renders the editor in a modal overlay. This is the default position. Overlays aren’t constrained to the CRUD’s size. This makes them ideal for complex forms. However, they do block the user from viewing and interacting with the Grid beneath.

Aside

The aside position displays the editor as an overlay next to the grid. Use this position when there is sufficient horizontal space to accommodate both the grid and the editor. Also, use it when it’s beneficial for the user to be able to view and interact with the grid while the editor is open. Aside positioning is also a good fit for single-column forms.

Open in a
new tab
<Crud editorPosition="aside" include="firstName, lastName, email, profession" items={items} />
Note
Grid Width
The opening and closing of an aside editor affect the grid’s width. Fixed-width columns are recommended to prevent them from resizing each time.

Bottom

The bottom position can be useful when the user needs to see as many columns in the grid as possible while editing, when horizontal space is limited, or when a wider editor form is desired.

Open in a
new tab
<Crud editorPosition="bottom" include="firstName, lastName, email, profession" items={items} />

When using a bottom-positioned editor, make sure there’s enough vertical space to fit comfortably both the grid and the editor. Incidentally, a bottom-positioned editor is generally an inappropriate option for longer forms.

Note
Small Viewports
On small viewports like mobile phones, the editor always opens as a full-screen overlay, regardless of this configuration.

Editor Content

The editor’s content is fully configurable, except for the header and footer.

Open in a
new tab
<Crud include="firstName, lastName, email, profession" items={items}>
  <FormLayout slot="form" style={{ maxWidth: '480px' }} responsiveSteps={responsiveSteps}>
    <TextField label="First name" {...crudPath('firstName')} required />
    <TextField label="Last name" {...crudPath('lastName')} required />
    <EmailField {...{ colspan: 2 }} label="Email" {...crudPath('email')} required />
    <ComboBox
      {...{ colspan: 2 }}
      label="Profession"
      {...crudPath('profession')}
      items={professions}
    />
  </FormLayout>
</Crud>

Editor Actions

The editor contains three Buttons: Delete, Cancel, and Save. The Delete shows a confirmation dialog asking the user to verify whether they wish to delete the item. Whereas the Cancel closes the editor unless there are unsaved changes. If so, a confirmation dialog is shown and the user can either discard the changes or go back to editing. The Save button, when clicked, saves the changes and closes the editor. This is disabled until a change is made.

Grid Replacement

CRUD’s default Grid is replaceable, which is useful when you wish to customize the Grid. An example of this might be if you want to place the edit Button in the first column, or to apply tooltips. See Grid documentation for details on configuring grids.

Open in a
new tab
<Crud include="firstName, lastName, email, profession" items={items}>
  <Grid slot="grid">
    <CrudEditColumn />
    <GridColumn path="firstName" header="First name" />
    <GridColumn path="lastName" header="Last name" />
    <GridColumn path="email" header="Email" />
    <GridColumn path="profession" header="Profession" />
  </Grid>
</Crud>
Note
Edit Column
You need to add explicitly an edit column to the replacement Grid to be able to edit items. Additionally, Grid doesn’t have sorting and filtering enabled by default.

Toolbar

Creating new items is done via the “New Item” Button in CRUD’s toolbar. Both the toolbar and its Button are customizable. For example, you can use the toolbar to display statistics such as the size of the dataset or the number of search results.

Open in a
new tab
<Crud
  include="firstName, lastName"
  items={items}
  onSizeChanged={() => {
    if (items.length !== employeeCount) {
      setEmployeeCount(items.length);
    }
  }}
>
  <HorizontalLayout slot="toolbar" style={{ alignItems: 'center', flexGrow: 1 }}>
    <span>
      Total: <b>{items.length}</b> employees
    </span>
  </HorizontalLayout>

  <Button theme="tertiary" slot="new-button">
    <Icon icon="vaadin:plus" slot="prefix" />
    New employee
  </Button>
</Crud>

Hiding the Toolbar

The toolbar can be hidden if it isn’t needed.

Open in a
new tab
{/* Adding the noToolbar prop hides the toolbar */}
<Crud include="firstName, lastName" items={items} onSizeChanged={() => {}} noToolbar />

Sorting & Filtering

By default, CRUD allows sorting and filtering of any column. For more information about sorting and filtering, see the Grid documentation.

Disabling Sorting & Filtering

Sorting and filtering can be disabled.

Open in a
new tab
return <Crud include="firstName, lastName, profession" noSort noFilter items={items} />;

Item Initialization

Newly created items can be initialized with data.

Open in a
new tab
const handleNewItem = (event: CrudNewEvent) => {
  // Cancel event to allow setting a custom item instance
  event.preventDefault();
  setEditedItem({
    email: '@vaadin.com',
    profession: 'Developer',
  });
};

return (
  <Crud
    include="firstName, lastName, email, profession"
    items={items}
    editedItem={editedItem}
    onNew={handleNewItem}
  />
);

Localization

CRUD supports full localization through customizable labels for its buttons and the title of the editor.

Open in a
new tab
const i18n = {
  newItem: 'Luo uusi',
  editItem: 'Muuta tietoja',
  saveItem: 'Tallenna',
  cancel: 'Peruuta',
  deleteItem: 'Poista...',
  editLabel: 'Muokkaa',
  confirm: {
    delete: {
      title: 'Poista kohde',
      content: 'Haluatko varmasti poistaa tämän kohteen? Poistoa ei voi perua.',
      button: {
        confirm: 'Poista',
        dismiss: 'Peruuta',
      },
    },
    cancel: {
      title: 'Hylkää muutokset',
      content: 'Kohteessa on tallentamattomia muutoksia',
      button: {
        confirm: 'Hylkää',
        dismiss: 'Peruuta',
      },
    },
  },
};

return (
  <Crud
    editorPosition="aside"
    include="firstName, lastName, email, profession"
    items={items}
    i18n={i18n}
  >
    <Grid slot="grid">
      <GridColumn path="firstName" header="Etunimi" />
      <GridColumn path="lastName" header="Sukunimi" />
      <GridColumn path="email" header="Sähköposti" />
      <GridColumn path="profession" header="Ammatti" />
      <CrudEditColumn />
    </Grid>

    <div slot="form">
      <TextField {...crudPath('firstName')} label="Etunimi" required />
      <TextField {...crudPath('lastName')} label="Sukunimi" required />
      <EmailField {...crudPath('email')} label="Sähköposti" required />
      <ComboBox
        {...crudPath('profession')}
        label="Ammatti"
        items={[...new Set(items.map((i) => i.profession))]}
      />
    </div>
  </Crud>
);
Component Usage Recommendation

Grid Pro

Component for showing and editing tabular data.

Grid

Component for showing tabular data.

Tree Grid

Component for showing hierarchical data.