排序使用jQuery一个ListView? [英] Sorting a listview using jQuery?

查看:165
本文介绍了排序使用jQuery一个ListView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,code只以升序order.How李项都升序进行排序,并通过使用此code降序排列的分拣<立GT;代码

 函数排序(名单,关键){
         $($(名单)获得()。反向())。每个(函数(外){
             VAR分拣=这一点;
             $($(名单)获得()。反向())。每个(函数(内部){
                 如果($(关键,这一点)的.text()localeCompare($(键,分选)的.text())大于0){
                     this.parentNode.insertBefore(sorting.parentNode.removeChild(排序),这一点);
                 }
             });
         });
     }

 的window.onload =函数(){
         VAR DESC = FALSE;
         的document.getElementById(ST codeDSC)。=的onclick功能(){
             // sortUnorderedList(项目list3,DESC);             排序('ul.conn1>李,TD code1');
             DESC =降序!;
             返回false;
         }

任何建议?

编辑:如果我给℃的然后将其排序降序排列。

EDIT2:

  $(文件)。就绪(函数(){
         VAR DESC = FALSE;
         的document.getElementById(ST codeDSC)。=的onclick功能(){
             // sortUnorderedList(项目list3,DESC);             排序('ul.conn>李,TD code');             DESC =降序!;
             返回false;
         }
         的document.getElementById(stNameDSC)。=的onclick功能(){
             // sortUnorderedList(项目list3,DESC);             排序('ul.conn>李','td.name');
             DESC =降序!;
             返回false;
         }
     });


解决方案

怎么样的jQuery插件:

 函数elemLocaleCompare(左,右){
    返回$(左)的.text()localeCompare($(右)的.text())。
}$ .fn.extend({
    sortlist一:功能(顺序){
        返回this.each(函数(){
            变量$名单= $(本),
                李= $ list.children(「李先生」)的toArray()排序(elemLocaleCompare)。            如果(li.length == 0)返回true;            order =序列|| $ list.data(nextOrder)|| ASC;            如果(为了==ASC)
                $ list.append(LI)。数据(nextOrder,说明);
            其他
                。$ list.append(li.reverse())的数据(nextOrder,ASC);
        });
    }
});

使用这样的:

  $(OL)sortlist中(ASC)。// 要么...
$(#someList)sortlist中(); //初始排序顺序,ASC是默认$(#someButton)。点击(函数(){
  $(#someList)sortlist中(); //没有参数 - >切换排序顺序
});

Here the code is only sorting the li items in ascending order.How to sort in both ascending and descending order by using this code Sorting <li> tags

 function sort(list, key) {
         $($(list).get().reverse()).each(function (outer) {
             var sorting = this;
             $($(list).get().reverse()).each(function (inner) {
                 if ($(key, this).text().localeCompare($(key, sorting).text()) > 0) {
                     this.parentNode.insertBefore(sorting.parentNode.removeChild(sorting), this);
                 }
             });
         });
     }

and

window.onload = function () {
         var desc = false;
         document.getElementById("stCodeDSC").onclick = function () {
             //             sortUnorderedList("list3", desc);

             sort('ul.conn1>li', 'td.code1');
             desc = !desc;
             return false;
         }

Any suggestion?

EDIT: If i give <0 then it is sorting in descending order..

EDIT2:

$(document).ready(function() {
         var desc = false;
         document.getElementById("stCodeDSC").onclick = function () {
             //             sortUnorderedList("list3", desc);

             sort('ul.conn>li', 'td.code');

             desc = !desc;
             return false;
         }
         document.getElementById("stNameDSC").onclick = function () {
             //             sortUnorderedList("list3", desc);

             sort('ul.conn>li', 'td.name');
             desc = !desc;
             return false;
         }
     });

解决方案

How about a jQuery plugin:

function elemLocaleCompare(left, right) {
    return $(left).text().localeCompare($(right).text());
}

$.fn.extend({
    sortList: function(order) {
        return this.each(function() {
            var $list = $(this),
                li = $list.children("li").toArray().sort(elemLocaleCompare);

            if (li.length == 0) return true;

            order = order || $list.data("nextOrder") || "asc";

            if (order == "asc")
                $list.append(li).data("nextOrder", "desc");
            else
                $list.append(li.reverse()).data("nextOrder", "asc");
        });
    }
});

Use like:

$("ol").sortList("asc");

// or...
$("#someList").sortList();  // initial sort order, "asc" is default

$("#someButton").click(function () {
  $("#someList").sortList();     // no argument -> toggle sort order
});
​

这篇关于排序使用jQuery一个ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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