按字母顺序对可排序列表中的元素进行排序(超过13个项目) [英] Alphabetically sort elements in sortable list with over 13 items

查看:95
本文介绍了按字母顺序对可排序列表中的元素进行排序(超过13个项目)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery UI创建多个相互关联的可排序列表

I'm using jQuery UI to create several, interconnected sortable lists

HTML:

  <div class="ulContainer">
    <div class="liHdr">Unassigned</div>
    <div class="ulWraper">
      <ul class="connectedSortableUl un ui-sortable">
        <li class="ui-state-default" style="">Frank Smith
          <input type="hidden" class="rowName" value="frank.smith">
          <input type="hidden" class="rowEmail" value="frank.smith@email.com">
          <input type="hidden" class="rowId" value="8VNe0ZT1v0">
          <input type="hidden" class="rowTeam" value="">
          <div class="panel-options" style="float:right;"><a href="#" class="sm"><i class="entypo-pencil"></i></a></div>
        </li>
      </ul>
    </div>
  </div>

以此类推....更多相同的

and so on....several more of the same

jQuery:

// create the sortable ui
$(".connectedSortableUl").sortable({
    connectWith: ".connectedSortableUl",
    placeholder: "ui-state-highlight",
    create: function(event, ui) {
        sort();
    }
});


// custom sort function to sort our sortable lists
function sort() {
    var sortableLists = $('.connectedSortableUl');
    $(sortableLists).each(function(index, element) {

        var listitems = $('li', element);

        listitems.sort(function(a, b) {
            var seeA = $(a).text().toUpperCase(); //just to see what's going on
            var seeB = $(b).text().toUpperCase(); //just to see what's going on
            return ($(a).text().toUpperCase() > $(b).text().toUpperCase())
        });
        $(element).append(listitems);
    });

}

  • 我希望此函数可以按字母A的顺序对每个列表进行排序 顶部,Z在底部
  • 具有短列表,13个或更少的项目,此功能可以正常工作.
  • 但是,如果列表中包含14个或更多项目,则某些内容将分解,并且列表现在可以按预期进行更长的排序了.
    • I expect this function to sort each list alphabetically with A at the top and Z at the bottom
    • With short lists, 13 or fewer items, this function works as expected.
    • However, if a list has 14 or more items, something breaks down and the list is now longer sorted as expected.
    • 为什么逻辑不能分解为13个以上的列表项,我该怎么做才能解决该问题或以其他方式获得所需的结果?

      Why does the logic break down with more than 13 list items, and what can I do to fix it or otherwise achieve the desired results?

      jsFiddle示例

      推荐答案

      将两个字符串与>进行比较将返回一个布尔值,但是sort需要一个数字.与localeCompare进行比较:

      Comparing two strings with > returns a boolean, but sort expects a number. Compare with localeCompare:

      listitems.sort(function (a, b) {
        var ta = $(a).text().toUpperCase();
        var tb = $(b).text().toUpperCase();
        return ta.localeCompare(tb);
      });
      

      这篇关于按字母顺序对可排序列表中的元素进行排序(超过13个项目)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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