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
Grid recalculateColumnWidths doesn't recalculate on first attempt #1864
Comments
Sample code:
|
A rather hacky workaround involves doing the recalculation via one extra client-server round trip. First, inside the Button click listener add:
Then add the method where the grid's recalculation would actually occur:
|
The same seems to happen with the tree grid and expanding/collapsing nodes there. The following code will not update the column widths on the first expand of the first node but on all following (easily seen by expanding, collapsing and expanding again the root node). public class DemoUI extends UI {
@Theme(Lumo.class)
@Route("")
public static class BaseRoute extends Div {
public BaseRoute() {
final TreeNode root = new TreeNode("root", null);
final TreeNode layer1 = new TreeNode("layer1 LONG STRING", root);
final TreeNode layer2 = new TreeNode("layer2 EVEN LONGER STRING", layer1);
final TreeGrid<TreeNode> grid = new TreeGrid<>();
grid.setWidth("300px");
grid.setItems(Collections.singletonList(root), TreeNode::getChildren);
grid.addHierarchyColumn(TreeNode::getName).setAutoWidth(true).setHeader("Hierarchy");
grid.addColumn(TreeNode::getName).setAutoWidth(true).setHeader("Name");
grid.addExpandListener(treeNodeTreeGridExpandEvent -> grid.recalculateColumnWidths());
grid.addCollapseListener(treeNodeTreeGridCollapseEvent -> grid.recalculateColumnWidths());
add(grid);
}
}
public static class TreeNode {
private final String name;
private final TreeNode parent;
private final List<TreeNode> children = new ArrayList<>();
public TreeNode(String name, TreeNode parent) {
this.name = name;
this.parent = parent;
if (this.parent != null) {
this.parent.addChildren(this);
}
}
public void addChildren(TreeNode... nodes) {
this.children.addAll(Arrays.asList(nodes));
}
public String getName() {
return name;
}
public TreeNode getParent() {
return parent;
}
public List<TreeNode> getChildren() {
return children;
}
}
} |
Just a nit pick, one should not extend UI in Vaadin 14
Corrected demo code below
|
As described in this post, Grid's
recalculateColumnWidths
does not resize the column when clicking a button that executes that code in its click listener. Rather, it resizes the column after a second click of that button. It should resize it after the first click.The text was updated successfully, but these errors were encountered: