Skip to content

Commit

Permalink
fix: update columns headers on attach
Browse files Browse the repository at this point in the history
Close #43
  • Loading branch information
paodb authored and mlopezFC committed Dec 12, 2024
1 parent 766b691 commit be4670f
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.vaadin.componentfactory.enhancedgrid;

import com.vaadin.componentfactory.enhancedgrid.bean.Person;
import com.vaadin.componentfactory.enhancedgrid.filtering.TextFilterField;
import com.vaadin.componentfactory.enhancedgrid.service.PersonService;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.dialog.Dialog;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.Paragraph;
import com.vaadin.flow.component.icon.VaadinIcon;
import com.vaadin.flow.router.Route;
import java.util.List;

/**
* Opening grid in a dialog.
*/
@Route(value = "in-dialog", layout = MainLayout.class)
public class GridInDialogView extends Div {

public GridInDialogView() {
add(new Paragraph("Opening grid in a dialog"));

EnhancedGrid<Person> grid = new EnhancedGrid<>();
grid.setItems(getItems());
grid.addColumn(Person::getFirstName).setHeader("First Name", new TextFilterField());
grid.addColumn(Person::getLastName).setHeader("Last Name").setSortable(true);
grid.setFilterIcon(VaadinIcon.FILTER.create());

Dialog dialog = new Dialog();
dialog.add(grid);
dialog.setWidth("800px");
dialog.setWidth("600px");

Button button = new Button("Open dialog to see grid", e -> dialog.open());
add(button);
}

private List<Person> getItems() {
PersonService personService = new PersonService();
return personService.fetchAll();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ public MainLayout() {
final RouterLink multiTree = new RouterLink("Multiple Selection Tree Grid", MultiTreeGridView.class);
final RouterLink lazySingleTree = new RouterLink("Lazy Single Selection Tree Grid", LazySingleTreeGridView.class);
final RouterLink lazyMultiTree = new RouterLink("Lazy Multiple Selection Tree Grid", LazyMultiTreeGridView.class);
final RouterLink customFilterIcon = new RouterLink("Custom filter icon grid", CustomFilterIconView.class);
final RouterLink customFilterIcon = new RouterLink("Custom filter icon grid", CustomFilterIconView.class);
final RouterLink gridInDialog = new RouterLink("Open Grid in Dialog", GridInDialogView.class);

final VerticalLayout menuLayout = new VerticalLayout(simpleSingleGrid, simpleMultiGrid, lazySingleGrid, lazyMultiGrid,
singleTree, multiTree, lazySingleTree, lazyMultiTree, customFilterIcon);
singleTree, multiTree, lazySingleTree, lazyMultiTree, customFilterIcon, gridInDialog);
addToDrawer(menuLayout);
addToNavbar(drawerToggle);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import java.util.Comparator;
import java.util.Optional;

import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.HasValueAndElement;
import com.vaadin.flow.component.dependency.JsModule;
Expand Down Expand Up @@ -91,14 +90,19 @@ public EnhancedColumn<T> setHeader(Component headerComponent, HasValueAndElement
}

/** @see Column#setHeader(Component) */
@Override
public EnhancedColumn<T> setHeader(Component headerComponent) {
if (this.isFilterable()) {
this.grid.getElement().executeJs("monkeyPatchHeaderRenderer(this.$connector, $0)", getInternalId());
}
return (EnhancedColumn<T>) super.setHeader(headerComponent);
}
@Override
public EnhancedColumn<T> setHeader(Component headerComponent) {
this.renderHeader();
return (EnhancedColumn<T>) super.setHeader(headerComponent);
}

protected void renderHeader() {
if (this.isFilterable()) {
this.grid.getElement().executeJs("monkeyPatchHeaderRenderer(this.$connector, $0)",
getInternalId());
}
}

/**
* @see Column#setHeader(String)
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,11 @@ private void updateFilterIcon() {
@Override
protected void onAttach(AttachEvent attachEvent) {
super.onAttach(attachEvent);
for(Column<T> column : getColumns()) {
EnhancedColumn<T> enhancedColumn = (EnhancedColumn<T>)column;
enhancedColumn.renderHeader();
enhancedColumn.updateFilterButtonStyle();
}
this.updateFilterIcon();
}
}
Expand Down

0 comments on commit be4670f

Please sign in to comment.