You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: knowledge-base/grid-scroll-to-selected-row.md
+261-4Lines changed: 261 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -22,11 +22,268 @@ res_type: kb
22
22
23
23
24
24
## Description
25
-
I would like to pre-select a row when the page is loaded, and I would like to show the grid with that row scrolled to the top, or sorted to appear on top.
25
+
26
+
I want to programmatically [select a row in the Grid]({%slug grid-selection-row%}) based on specific conditions in my code. Once selected, I’d like the Grid to automatically scroll to that row so it’s visible to the user.
26
27
27
28
## Solution
28
-
You can find a selected row in the grid markup by the `k-selected` CSS class it has, and use a bit of JavaScript to scroll to it - browsers provide the `scrollIntoView()` method for that.
29
29
30
-
With **row virtualization**, however, the selected row may not be rendered. That is why you need to find its position and scroll to it through the `Skip` parameter of the Grid.
30
+
The solution to select programatically a row in Grid and scroll to that selected row, depends on the Grid configuration.
31
+
32
+
### Grid with **[paging feature]({%slug components/grid/features/paging%})**
33
+
34
+
1. Ensure the Grid is on the same page as the selected row.
35
+
1. Invoke a JavaScript to make the browser scroll to the selected row into view. The browsers provide the <ahref="https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView"target="_blank">`scrollIntoView()` method</a> that does the scrolling. You can find a selected row in the grid markup by its `k-selected` CSS class.
36
+
37
+
### Grid with **[virtualization feature]({%slug components/grid/virtual-scrolling%})**
38
+
39
+
1. Use the [Grid state]({%slug grid-state%}).
40
+
1. Set the [`Skip` parameter]({%slug grid-state%}#information-in-the-grid-state) to the index of the item in the current data collection.
41
+
42
+
## Example
43
+
44
+
The example below offers comments in the code on some possible improvements.
45
+
46
+
>caption Select a Row in Grid Programmatically and Scroll to the Row
47
+
48
+
````CSHTML
49
+
@using Telerik.DataSource
50
+
@using Telerik.DataSource.Extensions
51
+
@inject IJSRuntime JsInterop
52
+
53
+
54
+
@* Move JavaScript code to a separate JS file in production *@
55
+
<script suppress-error="BL9992">
56
+
window.scrollToSelectedRow = function () {
57
+
var selectedRow = document.querySelector('.k-selected');
58
+
if (selectedRow) {
59
+
selectedRow.scrollIntoView();
60
+
}
61
+
}
62
+
</script>
63
+
64
+
<script suppress-error="BL9992">
65
+
window.scrollToFirstRow = function () {
66
+
var selectedRow = document.querySelector('.k-table-row.k-master-row');
0 commit comments