在Javascript中排序选择选项 - 基于同一行中的多个值 [英] Sorting Select Option in Javascript - Based on multiple values in same row

查看:94
本文介绍了在Javascript中排序选择选项 - 基于同一行中的多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个这样的列表框,并创建了一个标题栏,帮助对数据进行排序:

  ID名称
----------------------------------------------- -------------
ID1身份一
ID2身份二
ID3身份三
ID4身份四
----- -------------------------------------------------- ------

我需要根据ID和名称对上面的列表进行排序,意味着当用户点击ID时,应该根据ID对列表进行排序,反之亦然。但是问题出在数据层上,我们在一行中得到了这样的结果:

  ID1& nbsp;& nbsp; & nbsp:Idenitity One 

所以我们在选项标签中打印相同的结果, 。生成以上输出的代码为:

 < select id =selSortsize =10style =width:350px id =rpt> 
< option value =ID1>
ID1& nbsp; &安培; NBSP; Identity One
< / option>
< option value =ID2>
ID2& nbsp; &安培; NBSP;身份两个
< / option>
< option value =ID4>
ID4& nbsp; &安培; NBSP;身份四
< / option>
< option value =ID3>
ID3& nbsp; &安培; NBSP;身份三
< / option>



我的问题是为以上情况实施分拣设施?是的,我试图排序,但它是整理列表框,而不是基于单个值。至少现在我可以做什么格式化传入数据,以便将所有Ids放在一个数组中,并将名称放在另一个数组中,因此对此的所有指南/指针都表示赞赏。请让我知道,如果我没有解释清楚

编辑:



代码我尝试排序列表box

 函数sortSelect(selElem){
var tmpAry = new Array();
for(var i = 0; i< selElem.options.length; i ++){
tmpAry [i] = new Array();
tmpAry [i] [0] = selElem.options [i] .text;
tmpAry [i] [1] = selElem.options [i] .value;
}
tmpAry.sort();
while(selElem.options.length> 0){
selElem.options [0] = null; (tmpAry [i] [0],tmpAry [i] [1])的
}
变量($ i = 0; i< tmpAry.length; i ++) );
selElem.options [i] = op;
}
return;


解决方案

您可以使用自定义数组排序看看这个如何对ID选择基础进行排序?



这里的想法是创建一个arrray并根据它的值进行排序,然后根据排序值动态创建选项。



自定义排序代码

  source.sort(function(x,y){
if(isAsc)
返回y.id - x.id;
else
return x.id - y.id;
});


I have created a list box like this, and created a title bar which helps to sort data:

ID                 Name
------------------------------------------------------------
ID1                Identity One
ID2                Identity Two
ID3                Identity Three
ID4                Identity Four
-------------------------------------------------------------

I have a requirement to sort above list based on ID and also on Name, means when user clicks ID, it should sort list based on ID and vice versa. But the problem is from data layer we are getting like this in a single line:

ID1 &nbsp;&nbsp;&nbsp:Idenitity One

So we are printing the same in options tag, and getting output like above. Code to generate above output is:

 <select id="selSort" size="10" style="width:350px" id="rpt">
<option value = "ID1">
        ID1 &nbsp; &nbsp; Identity One
</option>
<option value = "ID2">
        ID2 &nbsp; &nbsp; Identity Two
</option>
<option value = "ID4">
        ID4 &nbsp; &nbsp; Identity Four
</option>
<option value = "ID3">
        ID3 &nbsp; &nbsp; Identity Three
</option>

My question is how do I implement sorting facility for above scenario? Yes, I tried to sort, but it is sorting entire list box, not based on single value. At-least now what can I do is format incoming data such way that put all Ids in one array and names in another, so any guide/pointers on this appreciated. Please let me know if I haven't explained clearly

EDIT:

Code I tried to sort a list box

function sortSelect(selElem) {
    var tmpAry = new Array();
    for (var i=0;i<selElem.options.length;i++) {
        tmpAry[i] = new Array();
        tmpAry[i][0] = selElem.options[i].text;
        tmpAry[i][1] = selElem.options[i].value;
    }
    tmpAry.sort();
    while (selElem.options.length > 0) {
        selElem.options[0] = null;
    }
    for (var i=0;i<tmpAry.length;i++) {
        var op = new Option(tmpAry[i][0], tmpAry[i][1]);
        selElem.options[i] = op;
    }
    return;
}

解决方案

You can use the custom array sort take a look at this How to sort select base on an id ?

The idea here is just create a arrray and sort that based on its value and than dynamically create the option based on the sorted value.

Custom sorting code

source.sort(function (x, y) {
  if (isAsc) 
     return y.id - x.id;
  else 
    return x.id - y.id;
});

这篇关于在Javascript中排序选择选项 - 基于同一行中的多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆