简体   繁体   English

客户端在<li>标签值内排序

[英]Client side Sorting inside < li> tag values

I have a HTML like this 我有这样的HTML

<div>
<ul id="ulId">
    <li id="liId1">
        <span id="spn1-Li1">YVariable1</span>
        <span id="spn2-Li1">XVariable2</span>
        <span id="spn3-Li1">ZVariable3</span> 
    </li>
    <li id="liId2">
        <span id="spn1-Li2">ZVariable1</span>
        <span id="spn2-Li2">YVariable2</span>
        <span id="spn3-Li2">XVariable3</span> 
    </li>
    <li id="liId3">
        <span id="spn1-Li3">XVariable1</span>
        <span id="spn2-Li3">ZVariable2</span>
        <span id="spn3-Li3">YVariable3</span> 
    </li>
</ul>
</div>

The view has like this 视图有这样

YVariable1 XVariable2 ZVariable3
ZVariable1 YVariable2 XVariable3
XVariable1 ZVariable2 YVariable3

But I need to sort the result based on the first span value, the remaining "< li>" should be same. 但是我需要根据第一个span值对结果进行排序,其余的“ <li>”应该相同。 Means the result should be as following on click of "sort by button". 表示单击“按按钮排序”时,结果应如下所示。

 XVariable1 ZVariable2 YVariable3
 YVariable1 XVariable2 ZVariable3
 ZVariable1 YVariable2 XVariable3

I need this in client side (javascript or jQuery). 我需要在客户端(javascript或jQuery)中使用。 I have tried in multiple ways, But I didn't get what I'm expecting. 我已经尝试了多种方式,但是我没有达到我的期望。

This code I have now, and same altered similar ways 我现在有这段代码,并且相同地更改了相似的方式

$('#btnOrderBy').click(function () {
    var data = [];
    $('#ulId li').each(function (item, value) {
        $('span', value).each(function (i, v) {
            data.push($(this).text());
        });
    });
    console.log(data.sort());
});

I used this code. 我用了这段代码。

$('#ulId').html($("#ulId li").sort(asc_sort));
function asc_sort(a, b){
    return ($(b).find('span:first-child').text()) < ($(a).find('span:first-child').text()) ? 1 : -1;    
}

link to jsFiddle 链接到jsFiddle

This is a duplicate of What is the easiest way to order a <UL>/<OL> in jQuery? 这与在jQuery中订购<UL> / <OL>的最简单方法什么重复 :

var items = $('li').get();
items.sort(function(a,b){
  var keyA = $(a).text();
  var keyB = $(b).text();

  if (keyA < keyB) return -1;
  if (keyA > keyB) return 1;
  return 0;
});
var ul = $('ul');
$.each(items, function(i, li){
  ul.append(li);
});

http://jsfiddle.net/kEB7p/ http://jsfiddle.net/kEB7p/

You can do it like this if you want to keep the LI 's in their position and only sort the spans 如果要保持LI的位置并且仅对跨度进行排序,则可以这样操作

First store the sorting order 首先存储排序顺序

var orderArr = [];
$('#ulId span:nth-child(1)').sort(function(a,b){
    var aa = $(a).text();
    var bb = $(b).text();
    orderArr.push(aa < bb ? -1 : aa > bb ? 1 : 0);
});

Then you want to get the spans from each LI , put them in an array, and sort them 然后,您要从每个LI获取跨度,将其放入数组中并对它们进行排序

var y = 0;
var spans = $('#ulId li').map(function(){
   return $('span',this); // or $(this).find('span')
}).sort(function(){ return orderArr[y++]; });

Then you can loop through and append them to the each LI 然后,您可以循环遍历并将其附加到每个LI

$('#ulId li').each(function(i,v){
   $(v).append(spans[i]);
});

FIDDLE 小提琴

Or if you want to sort the li's based on the span you can do this 或者,如果您要基于跨度对li进行排序,则可以执行此操作

$('#ulId li').sort(function(a,b){
    var aa = $(a).find('span:first-child').text();
    var bb = $(b).find('span:first-child').text();
    return aa<bb ? -1 : aa>bb ? 1 : 0;
}).appendTo('#ulId');

FIDDLE 小提琴

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

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