Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vaadin horizontal scroll on mouse wheel

I use a Vaadin Grid which has many columns (50) and only some rows (10). By default it scrolls vertically when the user uses the mouse wheel which is not desired in this scenario.

Is there a built in way to achieve horizontal scrolling on mouse wheel in Vaadin?

like image 976
hornisgrinde Avatar asked Dec 30 '25 08:12

hornisgrinde


1 Answers

Should be doable using a JS call. For example, for a V14 Grid,

Grid<String> grid = new Grid<>();
grid.setItems(Arrays.asList("one", "two", "three"));
grid.removeAllColumns();

for (int i = 0; i < 50; i++) {
    grid.addComponentColumn(Span::new);
}

grid.getElement().executeJs("let grid = $0;"
        + "let scroller = grid.$.scroller;"
        + "scroller.addEventListener('mousewheel', scrollHorizontally, false);"
        + "scroller.addEventListener('DOMMouseScroll', scrollHorizontally, false);"
        + "function scrollHorizontally(e) {"
        + "        e = window.event || e;"
        + "        let delta = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail)));"
        + "        grid._scrollLeft -= (delta * 40);"
        + "        e.preventDefault();"
        + "    }", grid);
like image 130
Tarek Oraby Avatar answered Jan 03 '26 23:01

Tarek Oraby



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!