#4-Registro-de-Información-Personal #16
@ -6,6 +6,7 @@ import org.springframework.data.domain.Page;
|
|||||||
import org.springframework.data.domain.PageRequest;
|
import org.springframework.data.domain.PageRequest;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
import org.springframework.data.domain.Sort;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -33,13 +34,13 @@ public class EmployeeService {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Page<Employee> getEmployeesPaginated(int pageNo, int pageSize) {
|
public Page<Employee> getEmployeesPaginated(int pageNo, int pageSize, String sortField, boolean ascending) {
|
||||||
Pageable pageable = PageRequest.of(pageNo - 1, pageSize);
|
Sort sort = ascending ? Sort.by(sortField).ascending() : Sort.by(sortField).descending();
|
||||||
|
Pageable pageable = PageRequest.of(pageNo - 1, pageSize, sort);
|
||||||
return employeeRepository.findAll(pageable);
|
return employeeRepository.findAll(pageable);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasNextPage(int currentPage, int pageSize) {
|
public boolean hasNextPage(int currentPage, int pageSize, String sortField, boolean ascending) {
|
||||||
Page<Employee> page = getEmployeesPaginated(currentPage, pageSize);
|
return getEmployeesPaginated(currentPage, pageSize, sortField, ascending).hasNext();
|
||||||
return page.hasNext();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,15 +2,11 @@ package com.primefactorsolutions.views;
|
|||||||
|
|
||||||
import com.primefactorsolutions.model.Employee;
|
import com.primefactorsolutions.model.Employee;
|
||||||
import com.primefactorsolutions.service.EmployeeService;
|
import com.primefactorsolutions.service.EmployeeService;
|
||||||
import com.vaadin.flow.component.ClickEvent;
|
|
||||||
import com.vaadin.flow.component.Component;
|
|
||||||
import com.vaadin.flow.component.ComponentEventListener;
|
|
||||||
import com.vaadin.flow.component.button.Button;
|
import com.vaadin.flow.component.button.Button;
|
||||||
import com.vaadin.flow.component.combobox.ComboBox;
|
import com.vaadin.flow.component.combobox.ComboBox;
|
||||||
import com.vaadin.flow.component.html.H2;
|
import com.vaadin.flow.component.html.H2;
|
||||||
import com.vaadin.flow.component.html.Main;
|
import com.vaadin.flow.component.html.Main;
|
||||||
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
|
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
|
||||||
import com.vaadin.flow.function.ValueProvider;
|
|
||||||
import com.vaadin.flow.router.PageTitle;
|
import com.vaadin.flow.router.PageTitle;
|
||||||
import com.vaadin.flow.router.Route;
|
import com.vaadin.flow.router.Route;
|
||||||
import com.vaadin.flow.spring.annotation.SpringComponent;
|
import com.vaadin.flow.spring.annotation.SpringComponent;
|
||||||
@ -25,77 +21,103 @@ import org.springframework.data.domain.Page;
|
|||||||
@Route(value = "/employees", layout = MainLayout.class)
|
@Route(value = "/employees", layout = MainLayout.class)
|
||||||
@PermitAll
|
@PermitAll
|
||||||
public class EmployeesListView extends Main {
|
public class EmployeesListView extends Main {
|
||||||
private final EmployeeService employeeService;
|
|
||||||
private int currentPage = 1;
|
|
||||||
private final int pageSize = 5;
|
|
||||||
private final Button previous;
|
|
||||||
private final Button next;
|
|
||||||
|
|
||||||
public EmployeesListView(final EmployeeService employeeService) {
|
private final EmployeeService employeeService;
|
||||||
|
private final int pageSize = 5;
|
||||||
|
private int currentPage = 1;
|
||||||
|
private String sortField = "firstName";
|
||||||
|
private boolean ascending = true;
|
||||||
|
private VGrid<Employee> grid;
|
||||||
|
private Button previous;
|
||||||
|
private Button next;
|
||||||
|
|
||||||
|
public EmployeesListView(EmployeeService employeeService) {
|
||||||
this.employeeService = employeeService;
|
this.employeeService = employeeService;
|
||||||
|
addComponents();
|
||||||
|
updateGrid();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addComponents() {
|
||||||
|
addTitle();
|
||||||
|
addOrderButtons();
|
||||||
|
configureGrid();
|
||||||
|
addNavigationButtons();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addTitle() {
|
||||||
final H2 title = new H2("Employees list");
|
final H2 title = new H2("Employees list");
|
||||||
alex marked this conversation as resolved
Outdated
|
|||||||
|
add(title);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addOrderButtons() {
|
||||||
final HorizontalLayout hl = new HorizontalLayout();
|
final HorizontalLayout hl = new HorizontalLayout();
|
||||||
final HorizontalLayout hf = new HorizontalLayout();
|
hl.add(createOrderButton("Employee List in Ascending Order", true));
|
||||||
final Button employeeListAscendingOrder = new Button("Employee List in Ascending Order");
|
hl.add(createOrderButton("Employee List in Descending Order", false));
|
||||||
employeeListAscendingOrder.addClickListener((ComponentEventListener<ClickEvent<Button>>) buttonClickEvent -> {
|
add(hl);
|
||||||
// no-op
|
}
|
||||||
|
|
||||||
|
private Button createOrderButton(String label, boolean isAscending) {
|
||||||
|
Button button = new Button(label);
|
||||||
|
button.addClickListener(event -> {
|
||||||
|
sortField = "firstName";
|
||||||
|
ascending = isAscending;
|
||||||
|
updateGrid();
|
||||||
});
|
});
|
||||||
hl.add(employeeListAscendingOrder);
|
return button;
|
||||||
final Button employeeListDescendingOrder = new Button("Employee List in Descending Order");
|
}
|
||||||
employeeListDescendingOrder.addClickListener((ComponentEventListener<ClickEvent<Button>>) buttonClickEvent -> {
|
private void configureGrid() {
|
||||||
// no-op
|
grid = new VGrid<>(Employee.class);
|
||||||
});
|
|
||||||
hl.add(employeeListDescendingOrder);
|
|
||||||
final VGrid<Employee> grid = new VGrid<>(Employee.class);
|
|
||||||
grid.setColumns("firstName", "lastName", "status");
|
grid.setColumns("firstName", "lastName", "status");
|
||||||
grid.setAllRowsVisible(true);
|
grid.setAllRowsVisible(true);
|
||||||
grid.addComponentColumn((ValueProvider<Employee, Component>) employee -> {
|
grid.addComponentColumn(employee -> createStatusComboBox()).setHeader("Change Status");
|
||||||
|
grid.addComponentColumn(employee -> createEditButton());
|
||||||
|
grid.addComponentColumn(employee -> createSaveButton());
|
||||||
|
add(grid);
|
||||||
|
}
|
||||||
|
|
||||||
|
private ComboBox<String> createStatusComboBox() {
|
||||||
ComboBox<String> statusComboBox = new ComboBox<>();
|
ComboBox<String> statusComboBox = new ComboBox<>();
|
||||||
statusComboBox.setItems("Active", "Inactive");
|
statusComboBox.setItems("Active", "Inactive");
|
||||||
return statusComboBox;
|
return statusComboBox;
|
||||||
}).setHeader("Change Status");
|
}
|
||||||
grid.addComponentColumn((ValueProvider<Employee, Component>) employee -> {
|
|
||||||
final Button edit = new Button("Edit");
|
private Button createEditButton() {
|
||||||
edit.addClickListener((ComponentEventListener<ClickEvent<Button>>) buttonClickEvent -> {
|
return new Button("Edit");
|
||||||
// no-op
|
}
|
||||||
});
|
private Button createSaveButton() {
|
||||||
return edit;
|
return new Button("Save");
|
||||||
melina.gutierrez marked this conversation as resolved
Outdated
alex
commented
no es una buena practica acceder a las dependencies del service. Veo que aca el problema es que la clase EmployeeService tiene anotacion @Data. Porfavor quitar la anotacion @Data de EmployeeService y crear metodo findAllEmployees si es necesario. PERO por que es necesario cargar TODOS los empleados aca? la tabla ya carga los empleados con paginacion. no es una buena practica acceder a las dependencies del service. Veo que aca el problema es que la clase EmployeeService tiene anotacion @Data. Porfavor quitar la anotacion @Data de EmployeeService y crear metodo findAllEmployees si es necesario.
PERO por que es necesario cargar TODOS los empleados aca? la tabla ya carga los empleados con paginacion.
|
|||||||
});
|
}
|
||||||
grid.addComponentColumn((ValueProvider<Employee, Component>) employee -> {
|
|
||||||
final Button save = new Button("Save");
|
private void addNavigationButtons() {
|
||||||
save.addClickListener((ComponentEventListener<ClickEvent<Button>>) buttonClickEvent -> {
|
final HorizontalLayout hf = new HorizontalLayout();
|
||||||
// no-op
|
previous = createNavigationButton("Previous", -1);
|
||||||
});
|
next = createNavigationButton("Next", 1);
|
||||||
return save;
|
hf.add(previous, next, createAddEmployeeButton());
|
||||||
});
|
add(hf);
|
||||||
this.previous = new Button("Previous");
|
}
|
||||||
this.previous.addClickListener((ComponentEventListener<ClickEvent<Button>>) buttonClickEvent -> {
|
|
||||||
if (currentPage > 1) {
|
private Button createNavigationButton(String label, int increment) {
|
||||||
|
Button button = new Button(label);
|
||||||
|
button.addClickListener(event -> changePage(increment));
|
||||||
|
return button;
|
||||||
|
}
|
||||||
|
private void changePage(int increment) {
|
||||||
|
if (increment < 0 && currentPage > 1) {
|
||||||
currentPage--;
|
currentPage--;
|
||||||
updateGrid(grid);
|
} else if (increment > 0 && employeeService.hasNextPage(currentPage, pageSize, sortField, ascending)) {
|
||||||
}
|
|
||||||
});
|
|
||||||
hf.add(this.previous);
|
|
||||||
this.next = new Button("Next");
|
|
||||||
this.next.addClickListener((ComponentEventListener<ClickEvent<Button>>) buttonClickEvent -> {
|
|
||||||
if (employeeService.hasNextPage(currentPage, pageSize)) {
|
|
||||||
currentPage++;
|
currentPage++;
|
||||||
updateGrid(grid);
|
|
||||||
}
|
}
|
||||||
});
|
updateGrid();
|
||||||
hf.add(this.next);
|
|
||||||
final Button addEmployee = new Button("Add Employee");
|
|
||||||
addEmployee.addClickListener((ComponentEventListener<ClickEvent<Button>>) buttonClickEvent -> {
|
|
||||||
this.getUI().flatMap(ui -> ui.navigate(EmployeeView.class, "new"));
|
|
||||||
});
|
|
||||||
hf.add(addEmployee);
|
|
||||||
add(title, hl, grid, hf);
|
|
||||||
updateGrid(grid);
|
|
||||||
}
|
}
|
||||||
private void updateGrid(VGrid<Employee> grid) {
|
private Button createAddEmployeeButton() {
|
||||||
Page<Employee> page = employeeService.getEmployeesPaginated(currentPage, pageSize);
|
return new Button("Add Employee", event ->
|
||||||
|
getUI().flatMap(ui -> ui.navigate(EmployeeView.class, "new"))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
private void updateGrid() {
|
||||||
|
Page<Employee> page = employeeService.getEmployeesPaginated(currentPage, pageSize, sortField, ascending);
|
||||||
grid.setItems(page.getContent());
|
grid.setItems(page.getContent());
|
||||||
this.previous.setEnabled(currentPage > 1);
|
previous.setEnabled(currentPage > 1);
|
||||||
this.next.setEnabled(page.hasNext());
|
next.setEnabled(page.hasNext());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user
no es necesario agregar esto porque ya existe el boton edit que navega a la vista edit.