jQuery 排序 DIVS 第二部分

发布于 2024-12-11 08:19:41 字数 1360 浏览 0 评论 0原文

上一个问题中,我询问如何按 2 个参数对 div 进行排序:名称或值、升序或降序。 我用我得到的想法编写了所有代码,除了最后两个问题之外,我所有的问题都得到了解决。

这就是我所做的...为了对 div 进行排序,首先我连接值和 id 并拥有一个如下所示的数组:(

window.sortval = ["76#box1", "71#box122", "125#box4"];

我使用 window. 以确保其在全局范围内) 第一部分是我想要排序的数值,第二部分是 DIV 的 ID。

问题 1 是如果我运行

window.sortval.sort(function(a,b){return a - b})

它不会得到排序。

-- 为了继续我的实验,我使用浏览器控制台并手动对数组进行排序。现在我有第二个问题。我要用来进行实际排序的代码不起作用:

我有

<div id="container">
    <div class="sortable" id="box1" rel="76" data-rel="Name One">
        [some inside html including a table and divs]
    </div>
    <div class="sortable" id="box122" rel="71" data-rel="Name Two">
        [some inside html including a table and divs]
    </div>
    <div class="sortable" id="box4" rel="125" data-rel="Name Three">
        [some inside html including a table and divs]
    </div>
</div>

,然后当我运行时

//get only the id part of each value in the array
var ids = $.map( window.sortname, function(val, i) {
  return val.substring(val.indexOf('#'));
}); 

//sort the DOM
var cont = $('#container');
    $.each(ids, function(i, v) {
    cont.append($(v));
});

没有任何反应,甚至没有错误

In a previous question I asked how to sort divs by 2 parameters: name or value, asc or desc.
I wrote all the code with the idead I got and I got all solved but 2 last issues.

This is what I do... To sort the divs, first I concatenate the the value and id and have an array like this:

window.sortval = ["76#box1", "71#box122", "125#box4"];

(I use window. to make sure its in the global scope)
The first part is the numeric value I want to sort by and the second the ID of the DIV.

ISSUE 1 is that if I run

window.sortval.sort(function(a,b){return a - b})

it doesn't get sorted.

--
To go on with my experiment I used the browser console and sorted the array by hand. Now I have the second issue. The code I was going to use to do the actual sorting doesn't work:

I have

<div id="container">
    <div class="sortable" id="box1" rel="76" data-rel="Name One">
        [some inside html including a table and divs]
    </div>
    <div class="sortable" id="box122" rel="71" data-rel="Name Two">
        [some inside html including a table and divs]
    </div>
    <div class="sortable" id="box4" rel="125" data-rel="Name Three">
        [some inside html including a table and divs]
    </div>
</div>

and then when I run

//get only the id part of each value in the array
var ids = $.map( window.sortname, function(val, i) {
  return val.substring(val.indexOf('#'));
}); 

//sort the DOM
var cont = $('#container');
    $.each(ids, function(i, v) {
    cont.append($(v));
});

Nothing happens, not even an error

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

執念 2024-12-18 08:19:41

如果您不需要该数组用于其他用途,这将对您的 .sortable div 进行排序并将它们放入 #container 中:

var sorted = $(".sortable").sort(function(a,b) {
    return $(a).attr("rel") - $(b).attr("rel");
});
var cont = $("#container");
$.each(sorted,function(i,el) {
    cont.append(el);
});

http://jsfiddle.net/Gx5qw/

If you don't need the array for something else, this will sort your .sortable divs and put them inside #container:

var sorted = $(".sortable").sort(function(a,b) {
    return $(a).attr("rel") - $(b).attr("rel");
});
var cont = $("#container");
$.each(sorted,function(i,el) {
    cont.append(el);
});

http://jsfiddle.net/Gx5qw/

莳間冲淡了誓言ζ 2024-12-18 08:19:41

这就是您所需要的..

window.sortval = ["76#box1", "71#box122", "125#box4"];


// sort based on the numeric value before the id
window.sortval.sort(function(a,b){
   return a.substring(0, a.indexOf('#')) - b.substring(0, b.indexOf('#'));
}); 

//sort the DOM based on the ordered array by using the ids
var cont = $('#container');
    $.each(window.sortval, function(i, v) {
    cont.append($( v.substring(v.indexOf('#')) ));
});

演示位于 http://jsfiddle.net/Wdybs/1/

This is what you need..

window.sortval = ["76#box1", "71#box122", "125#box4"];


// sort based on the numeric value before the id
window.sortval.sort(function(a,b){
   return a.substring(0, a.indexOf('#')) - b.substring(0, b.indexOf('#'));
}); 

//sort the DOM based on the ordered array by using the ids
var cont = $('#container');
    $.each(window.sortval, function(i, v) {
    cont.append($( v.substring(v.indexOf('#')) ));
});

demo at http://jsfiddle.net/Wdybs/1/

顾挽 2024-12-18 08:19:41

这看起来是一种相当迂回的按两个键排序的方法。您应该能够使用带有自定义排序函数的标准 sort() 来实现这一点。

这是一种方法,首先使用 rel 属性排序,然后使用 id

var cnt = $("#container");
var sorted = $(".sortable", cnt).sort(function(a, b) {
    // (optional), cache vars so we don't keep calling $(...)
    var $a = $(a), $b = $(b);
    var a_id = $a.attr("id"), b_id = $b.attr("id");
    var a_rel = $a.attr("rel"), b_rel = $b.attr("rel");

    if (a_rel == b_rel) { // if rel equal, sort by id
        return (a_id > b_id) ? 1 : (a_id < b_id) ? -1 : 0; // String comparison
    } else {
        return a_rel - b_rel; // Numeric comparison
    }
});

// reinsert in sorted order
$.each(sorted, function(i, e) { cnt.append(e); }); 

演示:http://jsfiddle.net/8F9Uv/

That looks like a rather roundabout way to sort by two keys. You should be able to that using a standard sort() with a custom sort function.

Here's one way to do it, sorting first with the rel attribute followed by the id:

var cnt = $("#container");
var sorted = $(".sortable", cnt).sort(function(a, b) {
    // (optional), cache vars so we don't keep calling $(...)
    var $a = $(a), $b = $(b);
    var a_id = $a.attr("id"), b_id = $b.attr("id");
    var a_rel = $a.attr("rel"), b_rel = $b.attr("rel");

    if (a_rel == b_rel) { // if rel equal, sort by id
        return (a_id > b_id) ? 1 : (a_id < b_id) ? -1 : 0; // String comparison
    } else {
        return a_rel - b_rel; // Numeric comparison
    }
});

// reinsert in sorted order
$.each(sorted, function(i, e) { cnt.append(e); }); 

Demo: http://jsfiddle.net/8F9Uv/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文