Sort MultiSelectFor data

1 Answer 34 Views
MultiSelect Sortable
Yoana
Top achievements
Rank 1
Yoana asked on 09 Nov 2023, 09:46 AM

Hey, I'm trying to sort the data in the MultiSelectFor, so it can be displayed in alphabetical order.


                            @(Html.Kendo().MultiSelectFor(model => model.UserIds)
                                .HtmlAttributes(new { @class = "large" })
                                .Placeholder("Select users ...")
                                .DataValueField("Value")
                                .DataTextField("Text")
                                .AutoClose(false)
                                .DataSource(.DataSource(ds => ds.Read("AvailableUsers", "UserApi"))
                                 )

I've searched for this issue and tried some suggested things but with no success: 


                            @(Html.Kendo().MultiSelectFor(model => model.UserIds)
                                .HtmlAttributes(new { @class = "large" })
                                .Placeholder("Select users ...")
                                .DataValueField("Value")
                                .DataTextField("Text")
                                .AutoClose(false)
                                .DataSource(ds => ds.Custom()
                                            .Transport(t => t
                                                .Read(r => r
                                                    .Action("AvailableUsers", "UserApi")
                                                    .Data("{ any: false }")
                                                )
                                            )
                                            .Sort(s => s
                                                .Add("Value")
                                                .Descending()
                                            ))
                                
                            )


 public JsonResult AvailableUsers()
 {
     var userRepo = new UserRepository(ntfy);
    var result = userRepo.GetAll().Select(_ => new SelectListItem
           {
                Text = string.Format("{0} ({1} {2})", _.Username, _.Firstname, _.Lastname),
                Value = _.Id.ToString()
           });
     return this.Json(result, JsonRequestBehavior.AllowGet);
 }

1 Answer, 1 is accepted

Sort by
0
Anton Mironov
Telerik team
answered on 13 Nov 2023, 01:55 PM

Hi Yoana,

Thank you for the code snippets and the details provided.

In order to achieve the desired behavior, I would recommend setting a sorting configuration in the DataSource of the MultiSelect Component.

The following demo represents the implementation and the resulting behavior of the pointed soring:

Here is a dojo example of the same approach:

Feel free to give a try to the approach above and let me know if further assistance with the MVC syntax is needed.

Kind Regards,
Anton Mironov
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages. If you're new to the Telerik family, be sure to check out our getting started resources, as well as the only REPL playground for creating, saving, running, and sharing server-side code.

Tags
MultiSelect Sortable
Asked by
Yoana
Top achievements
Rank 1
Answers by
Anton Mironov
Telerik team
Share this question
or