Skip to content

Commit a682c77

Browse files
javier-godoypaodb
authored andcommitted
fix: fix column index calculation when cells are joined
Close #134
1 parent 96ab041 commit a682c77

File tree

5 files changed

+234
-5
lines changed

5 files changed

+234
-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: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
grid.prependHeaderRow();
56+
add(grid);
57+
}
58+
59+
@Override
60+
@ClientCallable
61+
public JsonValue $call(JsonObject invocation) {
62+
return HeaderFooterStylesCallables.super.$call(invocation);
63+
}
64+
65+
66+
@RequiredArgsConstructor
67+
private abstract class StylesWrapper {
68+
@Delegate
69+
private final GridStylesHelper styles;
70+
}
71+
72+
private final class HeaderCellWrapperImpl extends StylesWrapper implements HeaderCellWrapper {
73+
public HeaderCellWrapperImpl(GridStylesHelper styles) {
74+
super(styles);
75+
}
76+
}
77+
78+
private final class HeaderRowWrapperImpl extends StylesWrapper implements HeaderRowWrapper {
79+
private final HeaderRow row;
80+
81+
public HeaderRowWrapperImpl(HeaderRow row) {
82+
super(GridHelper.getHeaderStyles(grid, row));
83+
this.row = row;
84+
}
85+
86+
@Override
87+
public HeaderCellWrapper getCell(int columnIndex) {
88+
HeaderCell cell = row.getCell(grid.getColumns().get(columnIndex));
89+
return new HeaderCellWrapperImpl(grid.getHeaderStyles(cell));
90+
}
91+
92+
@Override
93+
public HeaderCellWrapper join(int... columnIndexes) {
94+
HeaderCell cell = row.join(IntStream.of(columnIndexes)
95+
.mapToObj(grid.getColumns()::get)
96+
.toArray(Column[]::new));
97+
cell.setText("join " + IntStream.of(columnIndexes)
98+
.mapToObj(Integer::toString)
99+
.collect(Collectors.joining()));
100+
return new HeaderCellWrapperImpl(GridHelper.getHeaderStyles(grid, cell));
101+
}
102+
103+
}
104+
105+
@Override
106+
public HeaderRowWrapper getRow(int rowIndex) {
107+
HeaderRow row = grid.getHeaderRows().get(rowIndex);
108+
return new HeaderRowWrapperImpl(row);
109+
}
110+
111+
}

0 commit comments

Comments
 (0)