Skip to content

Commit 0a4ab15

Browse files
kb(calendar): always selected dates - special days
1 parent a3a4f1c commit 0a4ab15

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
---
2+
title: SelectedDates and Double-click event
3+
description: Keep certain dates always selected in a Telerik Blazor Calendar as visual indicators to the user.
4+
type: how-to
5+
page_title: Always Selected Dates - Marked Days
6+
slug: calendar-always-selected-dates
7+
position:
8+
tags:
9+
ticketid: 1434214
10+
res_type: kb
11+
---
12+
13+
## Environment
14+
<table>
15+
<tbody>
16+
<tr>
17+
<td>Product</td>
18+
<td>Calendar for Blazor</td>
19+
</tr>
20+
</tbody>
21+
</table>
22+
23+
24+
## Description
25+
I can pre-load existing dates fine, but if I select a new date the pre-loaded dates vanish. Do I need to reload dates on each click?
26+
27+
I want to show certain dates as marked on the calendar so the user always sees them, or they are always selected.
28+
29+
## Suggested Workarounds
30+
You can keep a list of the items you want to denote as always selected, and ensure that they are always present in the selection, so the user effectively cannot deselect them. To do this, you need a separate collection that you will check against when the date selection changes. For example:
31+
32+
````CSHTML
33+
@* the origSelection contains the immutable list *@
34+
35+
<TelerikCalendar SelectionMode="@CalendarSelectionMode.Multiple" ValueChanged="@MultipleSelectionChangeHandler"
36+
DisabledDates="@DisabledDates" SelectedDates="@chosenDates" @bind-Date="@startDate" @ref="multipleSelCalendar">
37+
</TelerikCalendar>
38+
<br />
39+
@if (chosenDates != null && chosenDates.Count > 0)
40+
{
41+
<ul>
42+
@foreach (DateTime date in chosenDates)
43+
{
44+
<li>@date.ToString("dd MMM yyyy")</li>
45+
}
46+
</ul>
47+
}
48+
49+
@code {
50+
private DateTime startDate = new DateTime(2019, 4, 1);
51+
52+
private List<DateTime> DisabledDates = new List<DateTime>() { new DateTime(2019, 4, 1), new DateTime(2019, 4, 2) };
53+
54+
private List<DateTime> chosenDates { get; set; }
55+
private List<DateTime> origSelection { get; set; }
56+
57+
private Telerik.Blazor.Components.TelerikCalendar multipleSelCalendar;
58+
private void MultipleSelectionChangeHandler()
59+
{
60+
chosenDates = multipleSelCalendar.SelectedDates;
61+
62+
//ensure that, after user selection, the static ones you want are still selected
63+
foreach (DateTime origItem in origSelection)
64+
{
65+
if (!chosenDates.Contains(origItem))
66+
{
67+
chosenDates.Add(origItem);
68+
}
69+
}
70+
}
71+
72+
protected override async Task OnInitializedAsync()
73+
{
74+
origSelection = new List<DateTime>()
75+
{
76+
new DateTime(2019, 4, 3),
77+
new DateTime(2019, 4, 5)
78+
};
79+
80+
if(chosenDates == null)
81+
{
82+
chosenDates = new List<DateTime>();
83+
}
84+
85+
// populate the desired initial selection in the actual selection
86+
foreach (DateTime item in origSelection)
87+
{
88+
chosenDates.Add(item);
89+
}
90+
}
91+
}
92+
````
93+
94+
## Notes
95+
When a Scheduler component becomes available, it may be better suited for such a task.
96+
97+
Also, you may want to see if the Calendar offers (day) templates when you are trying to implement such a feature, because if it does when you read this, you may be able to denote the special days without using the selection feature and tampering with the user actions.

0 commit comments

Comments
 (0)