-
Notifications
You must be signed in to change notification settings - Fork 223
docs: document CRUD customization API and editor behavior #5894
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a1437a5
docs: document CRUD customization API and editor behavior
peholmst cd98f9a
docs: use the CRUD product name in the styling page title
peholmst c19a861
docs: fix CRUD anchors and drop a stray render attribute
peholmst 744bdc3
docs: address review feedback on the CRUD page
peholmst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/main/java/com/vaadin/demo/component/crud/CrudCustomEditor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package com.vaadin.demo.component.crud; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| import com.vaadin.demo.DemoExporter; // hidden-source-line | ||
| import com.vaadin.demo.domain.Person; | ||
| import com.vaadin.flow.component.crud.Crud; | ||
| import com.vaadin.flow.component.grid.Grid; | ||
| import com.vaadin.flow.component.html.Div; | ||
| import com.vaadin.flow.router.Route; | ||
|
|
||
| @Route("crud-custom-editor") | ||
| public class CrudCustomEditor extends Div { | ||
|
|
||
| private Crud<Person> crud; | ||
|
|
||
| private String FIRST_NAME = "firstName"; | ||
| private String LAST_NAME = "lastName"; | ||
| private String EMAIL = "email"; | ||
| private String EDIT_COLUMN = "vaadin-crud-edit-column"; | ||
|
|
||
| public CrudCustomEditor() { | ||
| // tag::snippet[] | ||
| crud = new Crud<>(Person.class, new PersonCrudEditor()); | ||
|
|
||
| // The editor reports validation errors itself, so Save can stay | ||
| // enabled at all times. | ||
| crud.getSaveButton().setEnabled(true); | ||
| // end::snippet[] | ||
|
|
||
| setupGrid(); | ||
| setupDataProvider(); | ||
|
|
||
| add(crud); | ||
| } | ||
|
|
||
| private void setupGrid() { | ||
| Grid<Person> grid = crud.getGrid(); | ||
|
|
||
| // Only show these columns (all columns shown by default): | ||
| List<String> visibleColumns = Arrays.asList(FIRST_NAME, LAST_NAME, | ||
| EMAIL, EDIT_COLUMN); | ||
| grid.getColumns().forEach(column -> { | ||
| String key = column.getKey(); | ||
| if (!visibleColumns.contains(key)) { | ||
| grid.removeColumn(column); | ||
| } | ||
| }); | ||
|
|
||
| // Reorder the columns (alphabetical by default) | ||
| grid.setColumnOrder(grid.getColumnByKey(FIRST_NAME), | ||
| grid.getColumnByKey(LAST_NAME), grid.getColumnByKey(EMAIL), | ||
| grid.getColumnByKey(EDIT_COLUMN)); | ||
| } | ||
|
|
||
| private void setupDataProvider() { | ||
| PersonDataProvider dataProvider = new PersonDataProvider(); | ||
| crud.setDataProvider(dataProvider); | ||
| crud.addDeleteListener( | ||
| deleteEvent -> dataProvider.delete(deleteEvent.getItem())); | ||
| crud.addSaveListener( | ||
| saveEvent -> dataProvider.persist(saveEvent.getItem())); | ||
| } | ||
|
|
||
| public static class Exporter extends DemoExporter<CrudCustomEditor> { // hidden-source-line | ||
| } // hidden-source-line | ||
| } |
90 changes: 90 additions & 0 deletions
90
src/main/java/com/vaadin/demo/component/crud/CrudEditorButtons.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| package com.vaadin.demo.component.crud; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| import com.vaadin.demo.DemoExporter; // hidden-source-line | ||
| import com.vaadin.demo.domain.Person; | ||
| import com.vaadin.flow.component.crud.BinderCrudEditor; | ||
| import com.vaadin.flow.component.crud.Crud; | ||
| import com.vaadin.flow.component.crud.CrudEditor; | ||
| import com.vaadin.flow.component.formlayout.FormLayout; | ||
| import com.vaadin.flow.component.grid.Grid; | ||
| import com.vaadin.flow.component.html.Div; | ||
| import com.vaadin.flow.component.textfield.EmailField; | ||
| import com.vaadin.flow.component.textfield.TextField; | ||
| import com.vaadin.flow.data.binder.Binder; | ||
| import com.vaadin.flow.router.Route; | ||
|
|
||
| @Route("crud-editor-buttons") | ||
| public class CrudEditorButtons extends Div { | ||
|
|
||
| private Crud<Person> crud; | ||
|
|
||
| private String FIRST_NAME = "firstName"; | ||
| private String LAST_NAME = "lastName"; | ||
| private String EMAIL = "email"; | ||
| private String EDIT_COLUMN = "vaadin-crud-edit-column"; | ||
|
|
||
| public CrudEditorButtons() { | ||
| crud = new Crud<>(Person.class, createEditor()); | ||
|
|
||
| // tag::snippet[] | ||
| // Records in this dataset are archived rather than removed, so the | ||
| // editor shouldn't offer a Delete action at all. There's no API for | ||
| // removing the Button, so hide it with CSS. | ||
| crud.getDeleteButton().getStyle().set("display", "none"); | ||
| // end::snippet[] | ||
|
|
||
| setupGrid(); | ||
| setupDataProvider(); | ||
|
|
||
| add(crud); | ||
| } | ||
|
|
||
| private CrudEditor<Person> createEditor() { | ||
| TextField firstName = new TextField("First name"); | ||
| TextField lastName = new TextField("Last name"); | ||
| EmailField email = new EmailField("Email"); | ||
| FormLayout form = new FormLayout(firstName, lastName, email); | ||
|
|
||
| Binder<Person> binder = new Binder<>(Person.class); | ||
| binder.forField(firstName).asRequired().bind(Person::getFirstName, | ||
| Person::setFirstName); | ||
| binder.forField(lastName).asRequired().bind(Person::getLastName, | ||
| Person::setLastName); | ||
| binder.forField(email).asRequired().bind(Person::getEmail, | ||
| Person::setEmail); | ||
|
|
||
| return new BinderCrudEditor<>(binder, form); | ||
| } | ||
|
|
||
| private void setupGrid() { | ||
| Grid<Person> grid = crud.getGrid(); | ||
|
|
||
| // Only show these columns (all columns shown by default): | ||
| List<String> visibleColumns = Arrays.asList(FIRST_NAME, LAST_NAME, | ||
| EMAIL, EDIT_COLUMN); | ||
| grid.getColumns().forEach(column -> { | ||
| String key = column.getKey(); | ||
| if (!visibleColumns.contains(key)) { | ||
| grid.removeColumn(column); | ||
| } | ||
| }); | ||
|
|
||
| // Reorder the columns (alphabetical by default) | ||
| grid.setColumnOrder(grid.getColumnByKey(FIRST_NAME), | ||
| grid.getColumnByKey(LAST_NAME), grid.getColumnByKey(EMAIL), | ||
| grid.getColumnByKey(EDIT_COLUMN)); | ||
| } | ||
|
|
||
| private void setupDataProvider() { | ||
| PersonDataProvider dataProvider = new PersonDataProvider(); | ||
| crud.setDataProvider(dataProvider); | ||
| crud.addSaveListener( | ||
| saveEvent -> dataProvider.persist(saveEvent.getItem())); | ||
| } | ||
|
|
||
| public static class Exporter extends DemoExporter<CrudEditorButtons> { // hidden-source-line | ||
| } // hidden-source-line | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this behavior documented in the CRUD Javadoc? If not, this might actually be a bug in the
CRUDcomponent.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this part is documented.
Crud.getSaveButton():and
Crud.setDirty(boolean):The two
@seeeach other, so the override is a deliberate, documented escape hatch.SaveButton.onEnabledStateChangedimplements it by overriding the web component's__isSaveBtnDisabled, andCrud.onAttachre-applies it, so it survives detach/attach.One caveat on the paragraph above this one, which is not in the Javadoc: "Validity doesn't factor into this: Save is enabled for invalid input, too." That comes from the implementation —
vaadin-crud-mixin.jshas__isSaveBtnDisabled(isDirty) { return !isDirty; }, and Flow'sCrud.onAttachsetsthis.__validate = function () { return true; }so client-side validation never gates the save. Server-sideCrudEditor.validate()is the only gate. Happy to drop that sentence if you would rather not pin down undocumented behavior.