-
Notifications
You must be signed in to change notification settings - Fork 79
Added new kb article dropdownlist-custom-sorting-order-groups #2423
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
NansiYancheva
merged 5 commits into
master
from
new-kb-dropdownlist-custom-sorting-order-groups-16dcd5ea38bb4e039b4d8cd3e22bbe11
Oct 16, 2024
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
085a594
Added new kb article dropdownlist-custom-sorting-order-groups
8e0735f
update kb
NansiYancheva 303fda6
Update knowledge-base/dropdownlist-custom-sorting-order-groups.md
NansiYancheva 927c2a1
change external reference
NansiYancheva 72b8618
update after review
NansiYancheva File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
116 changes: 116 additions & 0 deletions
116
knowledge-base/dropdownlist-custom-sorting-order-groups.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| --- | ||
| title: Custom Sorting Order for Groups in DropDownList | ||
| description: Learn how to apply a custom order to groups in the DropDownList for Blazor. | ||
| type: how-to | ||
| page_title: How to Set a Custom Grouping Order in Blazor DropDownList | ||
| slug: dropdownlist-custom-sorting-order-groups | ||
| tags: dropdownlist, blazor, telerik, grouping, custom order | ||
| res_type: kb | ||
| ticketid: 1666981, 1579415 | ||
| --- | ||
|
|
||
| ## Environment | ||
|
|
||
| <table> | ||
| <tbody> | ||
| <tr> | ||
| <td>Product</td> | ||
| <td>DropDownList for Blazor</td> | ||
| </tr> | ||
| <tr> | ||
| <td>Version</td> | ||
| <td>6.2.0</td> | ||
NansiYancheva marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| </tr> | ||
| </tbody> | ||
| </table> | ||
|
|
||
| ## Description | ||
|
|
||
| I am using the [grouping feature of the DropDownList]({%slug components/dropdownlist/grouping%}). My DropDownList model has a nested model as property. The DropDownList `GroupField` parameter is bound to a property of the nested model. I want the groups to appear in a non-alphabetical, custom order. How can I customize the grouping order in the DropDownList? | ||
|
|
||
NansiYancheva marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ## Solution | ||
|
|
||
| To sort the groups in a DropDownList by a custom order, perform a manual sorting operation in the [`OnRead` event]({%slug components/dropdownlist/events%}#onread) handler. Follow these steps: | ||
|
|
||
| 1. Create a list of strings that represents the values of the group headers. This list will determine the preferred sorting order. | ||
| 2. Cast the [`DataSourceResult`]({%slug common-features-data-binding-onread%}#event-argument) to [`AggregateFunctionsGroup`](/blazor-ui/api/Telerik.DataSource.AggregateFunctionsGroup). | ||
NansiYancheva marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 3. Sort the casted data using the [`Sort` method](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.sort?view=net-8.0) with a custom comparison function. | ||
NansiYancheva marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 4. Pass the sorted data as [`args.Data`]({%slug common-features-data-binding-onread%}#todatasourceresult-method). | ||
|
|
||
| >caption Apply a custom grouping order in the DropDownList | ||
| ````CSHTML | ||
| @using Telerik.DataSource.Extensions | ||
| @using Telerik.DataSource | ||
|
|
||
| <TelerikDropDownList TItem="@Product" | ||
| TValue="@int" | ||
| OnRead="@ReadItems" | ||
| @bind-Value="@SelectedValue" | ||
| GroupField="ProductCategory.CategoryName" | ||
| TextField="@nameof(Product.Description)" | ||
| ValueField="@nameof(Product.ProductId)" | ||
| DefaultText="Select a Product"> | ||
| </TelerikDropDownList> | ||
|
|
||
| @code { | ||
| private int SelectedValue { get; set; } | ||
| private List<Product> Products = new List<Product> | ||
| { | ||
| new Product { ProductId = 10, Description = "Juliet Jewelery Product", ProductCategory = new ProductCategory { CategoryId = 10, CategoryName = "Jewelery" } }, | ||
NansiYancheva marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| new Product { ProductId = 18, Description = "Medical Care Product", ProductCategory = new ProductCategory { CategoryId = 8, CategoryName = "Healthcare" } }, | ||
| new Product { ProductId = 19, Description = "QQ Tech Product", ProductCategory = new ProductCategory { CategoryId = 9, CategoryName = "Information Technology" } }, | ||
| new Product { ProductId = 10, Description = "Z Jewelery Product", ProductCategory = new ProductCategory { CategoryId = 10, CategoryName = "Jewelery" } }, | ||
| new Product { ProductId = 4, Description = "Delta Bond Product", ProductCategory = new ProductCategory { CategoryId = 4, CategoryName = "Defense" } }, | ||
| new Product { ProductId = 8, Description = "Health Care Product", ProductCategory = new ProductCategory { CategoryId = 8, CategoryName = "Healthcare" } }, | ||
| new Product { ProductId = 9, Description = "Tech Product", ProductCategory = new ProductCategory { CategoryId = 9, CategoryName = "Information Technology" } } | ||
| }; | ||
|
|
||
| protected async Task ReadItems(DropDownListReadEventArgs args) | ||
| { | ||
| await Task.Delay(200); | ||
|
|
||
| var preferredOrder = new List<string> { "Healthcare", "Information Technology", "Defense", "Jewelery" }; | ||
|
|
||
| var datasourceResult = Products.ToDataSourceResult(args.Request); | ||
|
|
||
| var sortedData = datasourceResult.Data.Cast<AggregateFunctionsGroup>().ToList(); | ||
| sortedData.Sort((a, b) => | ||
| { | ||
| int indexA = preferredOrder.IndexOf(a.Key.ToString()); | ||
| int indexB = preferredOrder.IndexOf(b.Key.ToString()); | ||
|
|
||
| if (indexA >= 0 && indexB >= 0) | ||
| { | ||
| return indexA.CompareTo(indexB); | ||
| } | ||
|
|
||
| if (indexA >= 0) return -1; | ||
| if (indexB >= 0) return 1; | ||
| return a.Key.ToString().CompareTo(b.Key.ToString()); | ||
| }); | ||
|
|
||
| args.Data = sortedData; | ||
| } | ||
|
|
||
| public class Product | ||
| { | ||
| public int ProductId { get; set; } | ||
| public string Description { get; set; } | ||
| public ProductCategory ProductCategory { get; set; } | ||
| } | ||
|
|
||
| public class ProductCategory | ||
| { | ||
| public int CategoryId { get; set; } | ||
| public string CategoryName { get; set; } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
|
|
||
| ## See Also | ||
|
|
||
| - [Telerik DropDownList for Blazor - Overview]({%slug components/dropdownlist/overview%}) | ||
NansiYancheva marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| - [OnRead Event in Telerik DropDownList]({%slug components/dropdownlist/events%}#onread) | ||
| - [Grouping in Telerik DropDownList]({%slug components/dropdownlist/grouping%}) | ||
| - [Custom Grouping Order in Grid]({%slug grid-custom-grouping-order%}) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.