format
This commit is contained in:
parent
dacf049162
commit
35c92fae5f
|
@ -12,31 +12,17 @@ import com.vaadin.flow.spring.annotation.SpringComponent;
|
||||||
import com.vaadin.flow.spring.annotation.UIScope;
|
import com.vaadin.flow.spring.annotation.UIScope;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
||||||
/**
|
|
||||||
* A simple example to introduce building forms. As your real application is probably much
|
|
||||||
* more complicated than this example, you could re-use this form in multiple places. This
|
|
||||||
* example component is only used in MainView.
|
|
||||||
* <p>
|
|
||||||
* In a real world application you'll most likely using a common super class for all your
|
|
||||||
* forms - less code, better UX.
|
|
||||||
*/
|
|
||||||
@SpringComponent
|
@SpringComponent
|
||||||
@UIScope
|
@UIScope
|
||||||
public class EmployeeEditor extends VerticalLayout implements KeyNotifier {
|
public class EmployeeEditor extends VerticalLayout implements KeyNotifier {
|
||||||
|
|
||||||
private final EmployeeRepository repository;
|
private final EmployeeRepository repository;
|
||||||
|
|
||||||
/**
|
|
||||||
* The currently edited employee
|
|
||||||
*/
|
|
||||||
private Employee employee;
|
private Employee employee;
|
||||||
|
|
||||||
/* Fields to edit properties in Employee entity */
|
|
||||||
TextField firstName = new TextField("First name");
|
TextField firstName = new TextField("First name");
|
||||||
TextField lastName = new TextField("Last name");
|
TextField lastName = new TextField("Last name");
|
||||||
|
|
||||||
/* Action buttons */
|
|
||||||
// TODO why more code?
|
|
||||||
Button save = new Button("Save", VaadinIcon.CHECK.create());
|
Button save = new Button("Save", VaadinIcon.CHECK.create());
|
||||||
Button cancel = new Button("Cancel");
|
Button cancel = new Button("Cancel");
|
||||||
Button delete = new Button("Delete", VaadinIcon.TRASH.create());
|
Button delete = new Button("Delete", VaadinIcon.TRASH.create());
|
||||||
|
@ -51,10 +37,8 @@ public class EmployeeEditor extends VerticalLayout implements KeyNotifier {
|
||||||
|
|
||||||
add(firstName, lastName, actions);
|
add(firstName, lastName, actions);
|
||||||
|
|
||||||
// bind using naming convention
|
|
||||||
binder.bindInstanceFields(this);
|
binder.bindInstanceFields(this);
|
||||||
|
|
||||||
// Configure and style components
|
|
||||||
setSpacing(true);
|
setSpacing(true);
|
||||||
|
|
||||||
save.getElement().getThemeList().add("primary");
|
save.getElement().getThemeList().add("primary");
|
||||||
|
@ -62,7 +46,6 @@ public class EmployeeEditor extends VerticalLayout implements KeyNotifier {
|
||||||
|
|
||||||
addKeyPressListener(Key.ENTER, e -> save());
|
addKeyPressListener(Key.ENTER, e -> save());
|
||||||
|
|
||||||
// wire action buttons to save, delete and reset
|
|
||||||
save.addClickListener(e -> save());
|
save.addClickListener(e -> save());
|
||||||
delete.addClickListener(e -> delete());
|
delete.addClickListener(e -> delete());
|
||||||
cancel.addClickListener(e -> editEmployee(employee));
|
cancel.addClickListener(e -> editEmployee(employee));
|
||||||
|
@ -90,28 +73,18 @@ public class EmployeeEditor extends VerticalLayout implements KeyNotifier {
|
||||||
}
|
}
|
||||||
final boolean persisted = c.getId() != null;
|
final boolean persisted = c.getId() != null;
|
||||||
if (persisted) {
|
if (persisted) {
|
||||||
// Find fresh entity for editing
|
|
||||||
employee = repository.findById(c.getId()).get();
|
employee = repository.findById(c.getId()).get();
|
||||||
} else {
|
} else {
|
||||||
employee = c;
|
employee = c;
|
||||||
}
|
}
|
||||||
|
|
||||||
cancel.setVisible(persisted);
|
cancel.setVisible(persisted);
|
||||||
|
|
||||||
// Bind employee properties to similarly named fields
|
|
||||||
// Could also use annotation or "manual binding" or programmatically
|
|
||||||
// moving values from fields to entities before saving
|
|
||||||
binder.setBean(employee);
|
binder.setBean(employee);
|
||||||
|
|
||||||
setVisible(true);
|
setVisible(true);
|
||||||
|
|
||||||
// Focus first name initially
|
|
||||||
firstName.focus();
|
firstName.focus();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setChangeHandler(ChangeHandler h) {
|
public void setChangeHandler(ChangeHandler h) {
|
||||||
// ChangeHandler is notified when either save or delete
|
|
||||||
// is clicked
|
|
||||||
changeHandler = h;
|
changeHandler = h;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
package com.baeldung;
|
package com.baeldung;
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
|
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
|
||||||
|
|
||||||
List<Employee> findByLastNameStartsWithIgnoreCase(String lastName);
|
List<Employee> findByLastNameStartsWithIgnoreCase(String lastName);
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package com.baeldung;
|
package com.baeldung;
|
||||||
|
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
import com.vaadin.flow.component.button.Button;
|
import com.vaadin.flow.component.button.Button;
|
||||||
import com.vaadin.flow.component.grid.Grid;
|
import com.vaadin.flow.component.grid.Grid;
|
||||||
import com.vaadin.flow.component.icon.VaadinIcon;
|
import com.vaadin.flow.component.icon.VaadinIcon;
|
||||||
|
@ -8,8 +10,6 @@ import com.vaadin.flow.component.orderedlayout.VerticalLayout;
|
||||||
import com.vaadin.flow.component.textfield.TextField;
|
import com.vaadin.flow.component.textfield.TextField;
|
||||||
import com.vaadin.flow.data.value.ValueChangeMode;
|
import com.vaadin.flow.data.value.ValueChangeMode;
|
||||||
import com.vaadin.flow.router.Route;
|
import com.vaadin.flow.router.Route;
|
||||||
import com.vaadin.flow.spring.annotation.UIScope;
|
|
||||||
import org.springframework.util.StringUtils;
|
|
||||||
|
|
||||||
@Route
|
@Route
|
||||||
public class MainView extends VerticalLayout {
|
public class MainView extends VerticalLayout {
|
||||||
|
@ -31,7 +31,6 @@ public class MainView extends VerticalLayout {
|
||||||
this.filter = new TextField();
|
this.filter = new TextField();
|
||||||
this.addNewBtn = new Button("New employee", VaadinIcon.PLUS.create());
|
this.addNewBtn = new Button("New employee", VaadinIcon.PLUS.create());
|
||||||
|
|
||||||
// build layout
|
|
||||||
HorizontalLayout actions = new HorizontalLayout(filter, addNewBtn);
|
HorizontalLayout actions = new HorizontalLayout(filter, addNewBtn);
|
||||||
add(actions, grid, editor);
|
add(actions, grid, editor);
|
||||||
|
|
||||||
|
@ -41,31 +40,23 @@ public class MainView extends VerticalLayout {
|
||||||
|
|
||||||
filter.setPlaceholder("Filter by last name");
|
filter.setPlaceholder("Filter by last name");
|
||||||
|
|
||||||
// Hook logic to components
|
|
||||||
|
|
||||||
// Replace listing with filtered content when user changes filter
|
|
||||||
filter.setValueChangeMode(ValueChangeMode.EAGER);
|
filter.setValueChangeMode(ValueChangeMode.EAGER);
|
||||||
filter.addValueChangeListener(e -> listEmployees(e.getValue()));
|
filter.addValueChangeListener(e -> listEmployees(e.getValue()));
|
||||||
|
|
||||||
// Connect selected Employee to editor or hide if none is selected
|
|
||||||
grid.asSingleSelect().addValueChangeListener(e -> {
|
grid.asSingleSelect().addValueChangeListener(e -> {
|
||||||
editor.editEmployee(e.getValue());
|
editor.editEmployee(e.getValue());
|
||||||
});
|
});
|
||||||
|
|
||||||
// Instantiate and edit new Employee the new button is clicked
|
|
||||||
addNewBtn.addClickListener(e -> editor.editEmployee(new Employee("", "")));
|
addNewBtn.addClickListener(e -> editor.editEmployee(new Employee("", "")));
|
||||||
|
|
||||||
// Listen changes made by the editor, refresh data from backend
|
|
||||||
editor.setChangeHandler(() -> {
|
editor.setChangeHandler(() -> {
|
||||||
editor.setVisible(false);
|
editor.setVisible(false);
|
||||||
listEmployees(filter.getValue());
|
listEmployees(filter.getValue());
|
||||||
});
|
});
|
||||||
|
|
||||||
// Initialize listing
|
|
||||||
listEmployees(null);
|
listEmployees(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
// tag::listEmployees[]
|
|
||||||
void listEmployees(String filterText) {
|
void listEmployees(String filterText) {
|
||||||
if (StringUtils.isEmpty(filterText)) {
|
if (StringUtils.isEmpty(filterText)) {
|
||||||
grid.setItems(employeeRepository.findAll());
|
grid.setItems(employeeRepository.findAll());
|
||||||
|
@ -73,6 +64,4 @@ public class MainView extends VerticalLayout {
|
||||||
grid.setItems(employeeRepository.findByLastNameStartsWithIgnoreCase(filterText));
|
grid.setItems(employeeRepository.findByLastNameStartsWithIgnoreCase(filterText));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// end::listEmployees[]
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue