|
| 1 | +--- |
| 2 | +title: Scroll to a Slot in the Scheduler Timeline View |
| 3 | +description: Learn how to scroll to a specific slot in the Timeline View of the Telerik Scheduler for Blazor. |
| 4 | +type: how-to |
| 5 | +page_title: How to Scroll to a Slot in the Scheduler Timeline View |
| 6 | +slug: scheduler-kb-scroll-to-timeline-slot |
| 7 | +tags: blazor, scheduler, timeline |
| 8 | +ticketid: 1680788 |
| 9 | +res_type: kb |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +<table> |
| 15 | + <tbody> |
| 16 | + <tr> |
| 17 | + <td>Product</td> |
| 18 | + <td>Scheduler for Blazor</td> |
| 19 | + </tr> |
| 20 | + </tbody> |
| 21 | +</table> |
| 22 | + |
| 23 | +## Description |
| 24 | + |
| 25 | +This KB article answers the following questions: |
| 26 | + |
| 27 | +* How to focus and highlight the current time slot on load? |
| 28 | +* How to auto scroll horizontally the Scheduler Timeline to the current time slot? |
| 29 | +* How to style the current time slot in the Scheduler Timeline? |
| 30 | + |
| 31 | +## Solution |
| 32 | + |
| 33 | +1. Set the [Scheduler `View` parameter](slug:scheduler-views-overview) with two-way binding or with a [`ViewChanged` handler](slug:scheduler-events#viewchanged). Thus the app will know which is the current view. |
| 34 | +1. Define the [Scheduler `OnCellRender` event](slug:scheduler-events#oncellrender). Set a custom CSS class with `args.Class` if `args.Start` matches the current or desired time. |
| 35 | +1. Use the custom CSS class to apply custom styles. |
| 36 | +1. Use [`querySelectorAll()`](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll) and the custom CSS class to find the slot element in the HTML DOM. |
| 37 | +1. Use [`scrollIntoView()`](https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView) to scroll to the slot. |
| 38 | + |
| 39 | +>caption Scrolling to a styled Scheduler Timeline slot |
| 40 | +
|
| 41 | +````RAZOR |
| 42 | +@inject IJSRuntime js |
| 43 | +
|
| 44 | +<TelerikScheduler Data="@SchedulerData" |
| 45 | + @bind-Date="@SchedulerDate" |
| 46 | + @bind-View="@SchedulerView" |
| 47 | + OnCellRender="@OnSchedulerCellRender" |
| 48 | + Width="80vw" |
| 49 | + Height="80vh"> |
| 50 | + <SchedulerViews> |
| 51 | + <SchedulerDayView /> |
| 52 | + <SchedulerWeekView /> |
| 53 | + <SchedulerMonthView /> |
| 54 | + <SchedulerTimelineView /> |
| 55 | + </SchedulerViews> |
| 56 | +</TelerikScheduler> |
| 57 | +
|
| 58 | +@* Move JavaScript to separate JS file *@ |
| 59 | +<script suppress-error="BL9992"> |
| 60 | + function scrollToSlot() { |
| 61 | + var slotsAfterCurrent = document.querySelectorAll(".current-slot + .k-slot-cell"); |
| 62 | + if (slotsAfterCurrent.length > 0) { |
| 63 | + slotsAfterCurrent[slotsAfterCurrent.length - 1].scrollIntoView({ behavior: "smooth" }); |
| 64 | + } |
| 65 | + } |
| 66 | +</script> |
| 67 | +
|
| 68 | +<style> |
| 69 | + .current-slot { |
| 70 | + background-color: var(--kendo-color-primary-subtle); |
| 71 | + } |
| 72 | +</style> |
| 73 | +
|
| 74 | +@code { |
| 75 | + private List<Appointment> SchedulerData { get; set; } = new(); |
| 76 | + private DateTime SchedulerDate { get; set; } = DateTime.Today; |
| 77 | +
|
| 78 | + private SchedulerView SchedulerView { get; set; } = SchedulerView.Timeline; |
| 79 | +
|
| 80 | + private bool ShouldScrollTimeline { get; set; } |
| 81 | +
|
| 82 | + private void OnSchedulerCellRender(SchedulerCellRenderEventArgs args) |
| 83 | + { |
| 84 | + if (SchedulerView == SchedulerView.Timeline && args.Start.Hour == DateTime.Now.Hour) |
| 85 | + { |
| 86 | + args.Class = "current-slot"; |
| 87 | + ShouldScrollTimeline = true; |
| 88 | + } |
| 89 | + } |
| 90 | +
|
| 91 | + protected override async Task OnAfterRenderAsync(bool firstRender) |
| 92 | + { |
| 93 | + if (firstRender || ShouldScrollTimeline) |
| 94 | + { |
| 95 | + await Task.Delay(1); // Wait for HTML to render |
| 96 | + ShouldScrollTimeline = false; |
| 97 | +
|
| 98 | + await js.InvokeVoidAsync("scrollToSlot"); |
| 99 | + } |
| 100 | +
|
| 101 | + await base.OnAfterRenderAsync(firstRender); |
| 102 | + } |
| 103 | +
|
| 104 | + public class Appointment |
| 105 | + { |
| 106 | + public Guid Id { get; set; } |
| 107 | + public string Title { get; set; } = string.Empty; |
| 108 | + public DateTime Start { get; set; } |
| 109 | + public DateTime End { get; set; } |
| 110 | + public bool IsAllDay { get; set; } |
| 111 | + public string Description { get; set; } = string.Empty; |
| 112 | +
|
| 113 | + public Appointment() |
| 114 | + { |
| 115 | + Id = Guid.NewGuid(); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | +```` |
| 120 | + |
| 121 | +## See Also |
| 122 | + |
| 123 | +* [Scheduler Events](slug:scheduler-events) |
0 commit comments