Skip to content

Commit e00bb22

Browse files
committed
fix: fix column index calculation when cells are joined
Close #134
1 parent 96ab041 commit e00bb22

File tree

5 files changed

+246
-5
lines changed

5 files changed

+246
-5
lines changed

src/main/java/com/flowingcode/vaadin/addons/gridhelpers/HeaderFooterStylesHelper.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* #%L
33
* Grid Helpers Add-on
44
* %%
5-
* Copyright (C) 2022 - 2024 Flowing Code
5+
* Copyright (C) 2022 - 2025 Flowing Code
66
* %%
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
@@ -133,11 +133,17 @@ private abstract class CellSelector<ROW, CELL> implements SelectorSupplier {
133133
private int getColumnIndex(int rowIndex) {
134134
ROW row = getRowSelector().getRow();
135135
int j = -1;
136+
137+
CELL last = null;
136138
for (Column<?> c : helper.getGrid().getColumns()) {
137139
if (c.isVisible()) {
138-
++j;
139-
if (getCell(row, c) == getCell()) {
140-
break;
140+
CELL curr = getCell(row, c);
141+
if (curr != last) {
142+
++j;
143+
last = curr;
144+
if (curr == getCell()) {
145+
break;
146+
}
141147
}
142148
}
143149
}

src/test/java/com/flowingcode/vaadin/addons/gridhelpers/it/GridHelperElement.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* #%L
33
* Grid Helpers Add-on
44
* %%
5-
* Copyright (C) 2022 - 2024 Flowing Code
5+
* Copyright (C) 2022 - 2025 Flowing Code
66
* %%
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
@@ -127,4 +127,15 @@ public int getOffsetHeight() {
127127
public String getHeightByRowsSize() {
128128
return (String) executeScript("return this.style.getPropertyValue('--height-by-rows')");
129129
}
130+
131+
public WebElement getHeaderRow(int rowIndex) {
132+
WebElement thead = $("*").id("header");
133+
List<WebElement> headerRows = thead.findElements(By.tagName("tr"));
134+
return headerRows.get(rowIndex);
135+
}
136+
137+
public WebElement getHeaderCell(int rowIndex, int columnIndex) {
138+
List<WebElement> headerCells = getHeaderRow(rowIndex).findElements(By.tagName("th"));
139+
return headerCells.get(columnIndex);
140+
}
130141
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*-
2+
* #%L
3+
* Grid Helpers Add-on
4+
* %%
5+
* Copyright (C) 2022 - 2025 Flowing Code
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package com.flowingcode.vaadin.addons.gridhelpers.it;
21+
22+
import com.flowingcode.vaadin.addons.gridhelpers.GridStylesHelper;
23+
import com.flowingcode.vaadin.testbench.rpc.RmiCallable;
24+
import com.flowingcode.vaadin.testbench.rpc.RmiRemote;
25+
26+
public interface HeaderFooterStylesCallables extends RmiCallable {
27+
28+
public interface HeaderRowWrapper extends GridStylesHelper, RmiRemote {
29+
HeaderCellWrapper getCell(int columnIndex);
30+
HeaderCellWrapper join(int... columnIndexes);
31+
}
32+
33+
public interface HeaderCellWrapper extends GridStylesHelper, RmiRemote { }
34+
35+
HeaderRowWrapper getRow(int rowIndex);
36+
37+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*-
2+
* #%L
3+
* Grid Helpers Add-on
4+
* %%
5+
* Copyright (C) 2022 - 2025 Flowing Code
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package com.flowingcode.vaadin.addons.gridhelpers.it;
21+
22+
import static org.junit.Assert.assertEquals;
23+
import com.flowingcode.vaadin.addons.gridhelpers.it.HeaderFooterStylesCallables.HeaderRowWrapper;
24+
import com.flowingcode.vaadin.testbench.rpc.HasRpcSupport;
25+
import com.vaadin.flow.component.grid.testbench.GridElement;
26+
import org.junit.Test;
27+
28+
29+
public class HeaderFooterStylesIT extends AbstractViewTest implements HasRpcSupport {
30+
31+
HeaderFooterStylesCallables $server = createCallableProxy(HeaderFooterStylesCallables.class);
32+
33+
GridHelperElement grid;
34+
35+
public HeaderFooterStylesIT() {
36+
super(HeaderFooterStylesView.ROUTE);
37+
}
38+
39+
@Override
40+
public void setup() throws Exception {
41+
super.setup();
42+
grid = new GridHelperElement($(GridElement.class).waitForFirst());
43+
}
44+
45+
@Test
46+
public void testHeaderVisible() {
47+
HeaderRowWrapper row0 = $server.getRow(0);
48+
row0.join(0, 1).setClassName("row0-cell0");
49+
row0.join(2, 3).setClassName("row0-cell1");
50+
51+
HeaderRowWrapper row1 = $server.getRow(1);
52+
for (int i = 0; i < 5; i++) {
53+
row1.getCell(i).setClassName("row1-cell" + i);
54+
}
55+
56+
assertEquals("row0-cell0", grid.getHeaderCell(0, 0).getAttribute("class"));
57+
assertEquals("row0-cell1", grid.getHeaderCell(0, 1).getAttribute("class"));
58+
59+
for (int i = 0; i < 5; i++) {
60+
assertEquals("row1-cell" + i, grid.getHeaderCell(1, i).getAttribute("class"));
61+
}
62+
}
63+
64+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*-
2+
* #%L
3+
* Grid Helpers Add-on
4+
* %%
5+
* Copyright (C) 2022 - 2025 Flowing Code
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package com.flowingcode.vaadin.addons.gridhelpers.it;
21+
22+
import com.flowingcode.vaadin.addons.gridhelpers.GridHelper;
23+
import com.flowingcode.vaadin.addons.gridhelpers.GridStylesHelper;
24+
import com.vaadin.flow.component.ClientCallable;
25+
import com.vaadin.flow.component.grid.Grid;
26+
import com.vaadin.flow.component.grid.Grid.Column;
27+
import com.vaadin.flow.component.grid.HeaderRow;
28+
import com.vaadin.flow.component.grid.HeaderRow.HeaderCell;
29+
import com.vaadin.flow.component.html.Div;
30+
import com.vaadin.flow.router.Route;
31+
import elemental.json.JsonObject;
32+
import elemental.json.JsonValue;
33+
import java.util.stream.Collectors;
34+
import java.util.stream.IntStream;
35+
import lombok.RequiredArgsConstructor;
36+
import lombok.experimental.Delegate;
37+
import lombok.experimental.ExtensionMethod;
38+
39+
@SuppressWarnings("serial")
40+
@Route(HeaderFooterStylesView.ROUTE)
41+
@ExtensionMethod(GridHelper.class)
42+
public class HeaderFooterStylesView extends Div implements HeaderFooterStylesCallables {
43+
44+
public static final String ROUTE = "it/styles";
45+
46+
private Grid<Integer> grid;
47+
48+
public HeaderFooterStylesView() {
49+
setSizeFull();
50+
getElement().getStyle().set("flex-grow", "1");
51+
grid = new Grid<>();
52+
for (int i = 0; i < 5; i++) {
53+
grid.addColumn(x -> x).setHeader("col " + i);
54+
}
55+
56+
grid.prependHeaderRow();
57+
58+
// HeaderRow row0 = ;
59+
// HeaderRow row1 = grid.prependHeaderRow();
60+
// HeaderCell cell01 = row1.join(col0, col1);
61+
// HeaderCell cell23 = row1.join(col2, col3);
62+
// grid.getHeaderStyles(row0.getCell(col0)).setClassName("cell0");
63+
// grid.getHeaderStyles(row0.getCell(col1)).setClassName("cell1");
64+
// grid.getHeaderStyles(row0).setClassName("row0");
65+
// grid.getHeaderStyles(cell01).setClassName("cell01");
66+
// grid.getHeaderStyles(cell23).setClassName("cell23");
67+
// grid.getHeaderStyles(row1).setClassName("row1");
68+
add(grid);
69+
}
70+
71+
@Override
72+
@ClientCallable
73+
public JsonValue $call(JsonObject invocation) {
74+
return HeaderFooterStylesCallables.super.$call(invocation);
75+
}
76+
77+
78+
@RequiredArgsConstructor
79+
private abstract class StylesWrapper {
80+
@Delegate
81+
private final GridStylesHelper styles;
82+
}
83+
84+
private final class HeaderCellWrapperImpl extends StylesWrapper implements HeaderCellWrapper {
85+
public HeaderCellWrapperImpl(GridStylesHelper styles) {
86+
super(styles);
87+
}
88+
}
89+
90+
private final class HeaderRowWrapperImpl extends StylesWrapper implements HeaderRowWrapper {
91+
private final HeaderRow row;
92+
93+
public HeaderRowWrapperImpl(HeaderRow row) {
94+
super(GridHelper.getHeaderStyles(grid, row));
95+
this.row = row;
96+
}
97+
98+
@Override
99+
public HeaderCellWrapper getCell(int columnIndex) {
100+
HeaderCell cell = row.getCell(grid.getColumns().get(columnIndex));
101+
return new HeaderCellWrapperImpl(grid.getHeaderStyles(cell));
102+
}
103+
104+
@Override
105+
public HeaderCellWrapper join(int... columnIndexes) {
106+
HeaderCell cell = row.join(IntStream.of(columnIndexes)
107+
.mapToObj(grid.getColumns()::get)
108+
.toArray(Column[]::new));
109+
cell.setText("join " + IntStream.of(columnIndexes)
110+
.mapToObj(Integer::toString)
111+
.collect(Collectors.joining()));
112+
return new HeaderCellWrapperImpl(GridHelper.getHeaderStyles(grid, cell));
113+
}
114+
115+
}
116+
117+
@Override
118+
public HeaderRowWrapper getRow(int rowIndex) {
119+
HeaderRow row = grid.getHeaderRows().get(rowIndex);
120+
return new HeaderRowWrapperImpl(row);
121+
}
122+
123+
}

0 commit comments

Comments
 (0)