Skip to content

Commit ef767f1

Browse files
Tsvetomir-HrTsvetomir-Hr
authored andcommitted
kb(MultiSelect): apply suggestions and improve example
1 parent 637976e commit ef767f1

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

knowledge-base/multiselect-limit-selection.md

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,37 @@ When using the [MultiSelect]({%slug multiselect-overview%}) component, it might
2929

3030
## Solution
3131

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.
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.
3333

3434
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.
3535

36-
````RAZOR
37-
@if (MultiValues.Count == 3)
38-
{
39-
<p style="color:red;">You've reached the maximum selected count</p>
40-
}
41-
36+
````CSHTML
4237
<TelerikMultiSelect Data="@MultiData"
4338
Value="@MultiValues"
44-
ValueChanged="@( (List<string> newValues) => OnMultiValueChanged(newValues) )">
39+
ValueChanged="@( (List<string> newValues) => OnMultiValueChanged(newValues) )"
40+
AutoClose="false"
41+
OnItemRender="@OnItemRenderHandler"
42+
Width="400px">
4543
</TelerikMultiSelect>
4644
45+
@if (MultiValues.Count == 3)
46+
{
47+
<span style="color:red;">Maximum selected items reached</span>
48+
}
49+
4750
@code {
48-
private List<string> MultiData { get; set; } = new List<string> {
49-
"Manager", "Developer", "QA", "Technical Writer", "Support Engineer"
51+
private static List<string> MultiData { get; set; } = new List<string> {
52+
"Manager", "Developer", "QA", "Technical Writer", "Support Engineer", "Software Architect", "Product Manager"
5053
};
54+
private List<string> MultiValues { get; set; } = new List<string>() { MultiData[0] };
5155
52-
private List<string> MultiValues { get; set; } = new List<string>() { "Developer" };
53-
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+
}
5463
private void OnMultiValueChanged(List<string> newValues)
5564
{
5665
if (newValues.Count <= 3)

0 commit comments

Comments
 (0)