From 637976ef97796c4913fded57444e84e6c7174e24 Mon Sep 17 00:00:00 2001 From: KB Bot Date: Tue, 19 Nov 2024 15:05:28 +0000 Subject: [PATCH 1/2] Added new kb article multiselect-limit-selection --- knowledge-base/multiselect-limit-selection.md | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 knowledge-base/multiselect-limit-selection.md diff --git a/knowledge-base/multiselect-limit-selection.md b/knowledge-base/multiselect-limit-selection.md new file mode 100644 index 0000000000..cfc61bf04a --- /dev/null +++ b/knowledge-base/multiselect-limit-selection.md @@ -0,0 +1,67 @@ +--- +title: Restrict the Number of Selected Items in the MultiSelect +description: Learn how to restrict the number of selectable items in the Telerik Blazor MultiSelect component. +type: how-to +page_title: How to Restrict the Number of Selected Items in MultiSelect for Blazor +slug: multiselect-kb-limit-selection +tags: multiselect, blazor, selection limit +res_type: kb +ticketid: 1670731 +--- + +## Environment + + + + + + + + +
ProductMultiSelect for Blazor
+ +## Description + +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: +- How to set a selection limit on MultiSelect in Blazor? +- How to prevent additional selections in MultiSelect after reaching a defined limit? +- How to notify users they've reached the maximum number of selectable items in MultiSelect? + +## Solution + +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. + +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. + +````RAZOR +@if (MultiValues.Count == 3) +{ +

You've reached the maximum selected count

+} + + + + +@code { + private List MultiData { get; set; } = new List { + "Manager", "Developer", "QA", "Technical Writer", "Support Engineer" + }; + + private List MultiValues { get; set; } = new List() { "Developer" }; + + private void OnMultiValueChanged(List newValues) + { + if (newValues.Count <= 3) + { + MultiValues = newValues; + } + } +} +```` + +## See Also + +- [MultiSelect Overview]({%slug multiselect-overview%}) +- [MultiSelect Events]({%slug multiselect-events%}) From ef767f12da502d54c1f57fd2a489e08409fc35cd Mon Sep 17 00:00:00 2001 From: Tsvetomir Hristov <106250052+Tsvetomir-Hr@users.noreply.github.com> Date: Tue, 26 Nov 2024 15:09:59 +0200 Subject: [PATCH 2/2] kb(MultiSelect): apply suggestions and improve example --- knowledge-base/multiselect-limit-selection.md | 33 ++++++++++++------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/knowledge-base/multiselect-limit-selection.md b/knowledge-base/multiselect-limit-selection.md index cfc61bf04a..8ea67aec4a 100644 --- a/knowledge-base/multiselect-limit-selection.md +++ b/knowledge-base/multiselect-limit-selection.md @@ -29,28 +29,37 @@ When using the [MultiSelect]({%slug multiselect-overview%}) component, it might ## Solution -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. +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. 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. -````RAZOR -@if (MultiValues.Count == 3) -{ -

You've reached the maximum selected count

-} - +````CSHTML + ValueChanged="@( (List newValues) => OnMultiValueChanged(newValues) )" + AutoClose="false" + OnItemRender="@OnItemRenderHandler" + Width="400px"> +@if (MultiValues.Count == 3) +{ + Maximum selected items reached +} + @code { - private List MultiData { get; set; } = new List { - "Manager", "Developer", "QA", "Technical Writer", "Support Engineer" + private static List MultiData { get; set; } = new List { + "Manager", "Developer", "QA", "Technical Writer", "Support Engineer", "Software Architect", "Product Manager" }; + private List MultiValues { get; set; } = new List() { MultiData[0] }; - private List MultiValues { get; set; } = new List() { "Developer" }; - + private void OnItemRenderHandler(MultiSelectItemRenderEventArgs args) + { + if (MultiValues.Count == 3 && !MultiValues.Contains(args.Item)) + { + args.Class = "k-disabled"; + } + } private void OnMultiValueChanged(List newValues) { if (newValues.Count <= 3)