简体   繁体   English

如何按字母顺序排序下拉列表

[英]How to sort alphabetically drop-down list

I am creating my own website in C# , and I am trying to sort the list of names of some people that I get from the database in alphabetical order (A - Z).我正在C#创建自己的网站,并且我正在尝试按字母顺序(A - Z)对从数据库中获得的一些人的姓名列表进行排序。

I leave a jsflidde with an example of what I have done and the function made in javacript with which I load the list of people in the drop-down list我留下了一个jsflidde示例,说明了我所做的事情以及用 javacript 制作的function ,我用它在下拉列表中加载人员列表

 function loadResponsable() { const url = document.getElementById("responsables").value; document.getElementById("AssignDiv").style.display = "block"; $.ajax({ url: url, type: "POST", dataType: "json", success: function(data) { const responsables = document.getElementById("ticketRespInput"); for (let idx in data) { if (data.hasOwnProperty(idx)) { const option = document.createElement("option"); option.innerHTML = data[idx]; option.value = idx; responsables.options.add(option); } } } }); };
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> <div class="form-group" name="asignar" id="AssignDiv"> <label class="required-field" name="asignar" id="lblAssignDiv" for="ticketRespInput">Asignado a:</label> <select onchange="validate(this)" id="ticketRespInput" name="assigned" class="form-control form-control-user validateable" style="width: 100%; padding: 0.375rem 0.75rem; height: 50px;" tabindex="-1" aria-hidden="true"> <option value="" disabled selected>Sin asignar</option> </select> </div>

UPDATE:更新:

I add server side code我添加服务器端代码

[HttpPost]
        public JsonResult LoadResponsables()
        {
            var groups = new List<string>();

            if (string.Equals(Session["tipo"].ToString(), "super") ||
                string.Equals(Session["tipo"].ToString(), "admin"))
                groups.AddRange(LdapGroupModel.GetAllLdapGoups().LdapGroupsList
                    .Select(ldapGroup => ldapGroup.LdapGroupsId));
            else
                groups.AddRange(LdapGroupModel.GetLdapGroupsFromArea(Session["area"].ToString()).LdapGroupFromArea
                    .Select(ldapGroup => ldapGroup.LdapGroupsId));

            return Json(LdapController.GetUsersByGroup(groups));
        }

Error:错误:

在此处输入图像描述

You can sort in ascending or descending order when you are fetching from database with SQL or no-SQL syntax and with javascript, you can sor the array 'data' with sort() function.当您使用 SQL 或无 SQL 语法从数据库中获取数据时,您可以按升序或降序排序,并且使用 javascript,您可以使用 sort() ZC1C425268E68385D1AB5074 对数组“数据”进行排序。

data.sort()

put this line before the for loop.将此行放在 for 循环之前。

There are a couple of ways you can do this.有几种方法可以做到这一点。 One option is to use Sort() on the server-side.一种选择是在服务器端使用Sort() Instead of代替

return Json(LdapController.GetUsersByGroup(groups));

You could do:你可以这样做:

return Json(LdapController.GetUsersByGroup(groups.Sort()));

But personally, I would dig into LdapGroupModel.GetAllLdapGoups() and find the root query that generates your list, and do the sorting there.但就个人而言,我会深入LdapGroupModel.GetAllLdapGoups()并找到生成列表的根查询,并在那里进行排序。 I like to sort the data as close to the data source as possible.我喜欢尽可能靠近数据源对数据进行排序。 But that's just me.但这只是我。 Either should work!要么应该工作!

EDIT:编辑:

Ok, my mistake.好吧,我的错。 Try this instead:试试这个:

groups.Sort();
return Json(LdapController.GetUsersByGroup(groups));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM