Skip to content

Commit e30cc6c

Browse files
github-actions[bot]KB BotTsvetomir-Hr
authored
Merge new-kb-multiselect-limit-selection-c8fda6ffa4544544bee101b95debb591-2543 into production (#2567)
* Added new kb article multiselect-limit-selection * kb(MultiSelect): apply suggestions and improve example --------- Co-authored-by: KB Bot <[email protected]> Co-authored-by: Tsvetomir Hristov <[email protected]>
1 parent a6634e2 commit e30cc6c

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
---
2+
title: Restrict the Number of Selected Items in the MultiSelect
3+
description: Learn how to restrict the number of selectable items in the Telerik Blazor MultiSelect component.
4+
type: how-to
5+
page_title: How to Restrict the Number of Selected Items in MultiSelect for Blazor
6+
slug: multiselect-kb-limit-selection
7+
tags: multiselect, blazor, selection limit
8+
res_type: kb
9+
ticketid: 1670731
10+
---
11+
12+
## Environment
13+
14+
<table>
15+
<tbody>
16+
<tr>
17+
<td>Product</td>
18+
<td>MultiSelect for Blazor</td>
19+
</tr>
20+
</tbody>
21+
</table>
22+
23+
## Description
24+
25+
When using the [MultiSelect]({%slug multiselect-overview%}) component, it might be necessary to limit the number of items a user can select. For example, restricting selection to a maximum of three items. This KB article also answers the following questions:
26+
- How to set a selection limit on MultiSelect in Blazor?
27+
- How to prevent additional selections in MultiSelect after reaching a defined limit?
28+
- How to notify users they've reached the maximum number of selectable items in MultiSelect?
29+
30+
## Solution
31+
32+
To restrict the number of selectable items in a MultiSelect component, use one-way binding with the `Value` parameter and the [`ValueChanged`]({%slug multiselect-events%}#valuechanged) event. This approach allows you to manually update the selected items collection and enforce a maximum selection limit.
33+
34+
Below is a demonstration of how to implement a selection limit. This example restricts the user to a maximum of three selections and informs them when they have reached this limit.
35+
36+
````CSHTML
37+
<TelerikMultiSelect Data="@MultiData"
38+
Value="@MultiValues"
39+
ValueChanged="@( (List<string> newValues) => OnMultiValueChanged(newValues) )"
40+
AutoClose="false"
41+
OnItemRender="@OnItemRenderHandler"
42+
Width="400px">
43+
</TelerikMultiSelect>
44+
45+
@if (MultiValues.Count == 3)
46+
{
47+
<span style="color:red;">Maximum selected items reached</span>
48+
}
49+
50+
@code {
51+
private static List<string> MultiData { get; set; } = new List<string> {
52+
"Manager", "Developer", "QA", "Technical Writer", "Support Engineer", "Software Architect", "Product Manager"
53+
};
54+
private List<string> MultiValues { get; set; } = new List<string>() { MultiData[0] };
55+
56+
private void OnItemRenderHandler(MultiSelectItemRenderEventArgs<string> args)
57+
{
58+
if (MultiValues.Count == 3 && !MultiValues.Contains(args.Item))
59+
{
60+
args.Class = "k-disabled";
61+
}
62+
}
63+
private void OnMultiValueChanged(List<string> newValues)
64+
{
65+
if (newValues.Count <= 3)
66+
{
67+
MultiValues = newValues;
68+
}
69+
}
70+
}
71+
````
72+
73+
## See Also
74+
75+
- [MultiSelect Overview]({%slug multiselect-overview%})
76+
- [MultiSelect Events]({%slug multiselect-events%})

0 commit comments

Comments
 (0)