Scrolling a Selected Item into View in the Drawer
Environment
| Product | Progress® Kendo UI® for Angular Drawer |
Description
I want to select a specific item in the Drawer and scroll to it when the items do not fit in the available height. The selected item must be visible to the user.
This Knowledge Base article also answers the following questions:
- How can I scroll to an item in the Angular Drawer?
- Does the Drawer have a
scrollTomethod? - How can I make a programmatically selected Drawer item visible?
Solution
The Drawer does not scroll automatically when you select an item, and it does not provide a scrollTo method. To reveal the selected item, scroll it into view with the browser API after the Drawer updates.
The following example demonstrates a Drawer with 30 items. Each button selects an item and scrolls the Drawer to it.
To implement the approach in your application:
-
Set a fixed height to the Drawer container so that the Drawer items overflow and the Drawer renders a scrollbar.
css.container { height: 250px; } -
Add an item-specific class through the
cssClassproperty of eachDrawerItem, for example,drawer-item-25.TSpublic items: DrawerItem[] = Array.from({ length: 30 }, (_, index) => ({ id: index + 1, text: `Item ${index + 1}`, cssClass: `drawer-item-${index + 1}`, selected: index === 0 })); -
To select an item programmatically, update the
itemscollection and set theselectedproperty of the target item totrue.TSthis.items = this.items.map((item) => ({ ...item, selected: item.id === id })); -
After the next render, find the item by its class in the Drawer element and call its
scrollIntoViewmethod.TSafterNextRender( () => { this.drawer().nativeElement.querySelector(`.drawer-item-${id}`)?.scrollIntoView({ block: 'nearest' }); }, { injector: this.injector } );