复杂的 jQuery div 排序

发布于 2024-12-11 12:10:13 字数 1516 浏览 0 评论 0原文

我正在尝试以不同的方式对一些 DIVS 进行排序,但我很迷失。我一直在尝试一些东西,但我不知道如何让它发挥作用。

我在容器内有一组具有相同类、不同 id、rels 和数据属性的不同 div。

<div id="container">
    <div class="sortable" id="div541" rel="1234" data-rel="Name One">
        [some inside html including a table and divs]
    </div>
    <div class="sortable" id="div354" rel="4321" data-rel="Name Two">
        [some inside html including a table and divs]
    </div>
    <div class="sortable" id="div763" rel="112233" data-rel="Name Three">
        [some inside html including a table and divs]
    </div>
</div>

然后我有一个

首先我想到了像 Quicksand 这样的 jQuery 插件,但它克隆了改变其 ID 的元素,我有几个元素在每个可排序的 div 中,我需要保留它们的 ID,以便从容器外部与它们进行交互。我搜索了其他插件,但它们都做了一些排序,但以不同的方式,我决定自己做这件事。

所以我做了一些谷歌搜索和 stackoverflowing,发现我可以通过执行以下操作对 div 进行排序:

 var ids = ['#div541', '#div354', '#541' ...];
 var cont = $('#resultados');
 $.each(ids, function(i, v) {
     cont.append($(v));
 });

但是在进行实际排序之前我必须对 ID 数组进行排序,这就是我迷失的地方。 在我继续之前,这是实现排序的最佳方法吗? container.append 部分会修改任何 ID 或重复项和 div 吗?

如果可以的话,可以帮我做一下吗?

我首先会做一个 $('.sortable).each() ,在其中创建一个 3“列”数组。然后我对 rel 或 data-rel 属性 ASC 或 DESC 进行排序,一旦数组排序完毕,我就用它来将 div 追加()回容器中。这样可以吗?我特别讨厌在 JavaScript 中使用数组,所以你能帮我制作一个函数来对其进行排序,然后按顺序恢复 ID 列表吗?

多谢!

I'm trying to sort some DIVS in different ways and I'm quite lost. I've been trying some stuff but I don't see how to get it to work.

I have a set of different divs with the same class, different ids, rels and data-attribute inside a container.

<div id="container">
    <div class="sortable" id="div541" rel="1234" data-rel="Name One">
        [some inside html including a table and divs]
    </div>
    <div class="sortable" id="div354" rel="4321" data-rel="Name Two">
        [some inside html including a table and divs]
    </div>
    <div class="sortable" id="div763" rel="112233" data-rel="Name Three">
        [some inside html including a table and divs]
    </div>
</div>

Then I have a <select> element in which the user select the sort type. Options are by numeric value "stored" in the rel attribute (ascending and descending) and by name "stored" in the data-rel attribute (ascending and descending as well).

First I thought of a jQuery plug-in like Quicksand but it clones the elements changing its ID and I have several elements inside each sortable div which I need to keep their ID to interact with them from outside the container. I searched other plug-ins but they all did some sorting but in a different way and I decided doing this myself.

So I did some googling and stackoverflowing and found that I can sort the divs by doing something like this:

 var ids = ['#div541', '#div354', '#541' ...];
 var cont = $('#resultados');
 $.each(ids, function(i, v) {
     cont.append($(v));
 });

But I have to sort the IDs array before doing the actual sorting and that's where I'm lost.
Before I go on, is this the best way of achieving the sorting? Will the container.append part modify any ID or duplicate and div?

If it's okay, can you please help me do it?

I would first do a $('.sortable).each() in wich I make a 3 "column" array. So then I sort the rel or the data-rel attribute ASC or DESC and once the array is sorted I use it to append() the divs back into the container. Is this okay? I hate using arrays specially in JavaScript, so could you please give me a hand to make a function to sort it and then get the list of IDs back in order?

Thanks a lot!

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

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

发布评论

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

评论(1

奢华的一滴泪 2024-12-18 12:10:13

一种有趣的排序方法是创建一个精心设计的字符串数组,其中包含排序后的属性值、sperarator 和元素的 ID。

在您的示例数据中,字符串数组应如下所示:

['Name One#div541', 'Name Two#div354', 'Name Three#div763', ...]

或者,如果您想按数值排序:

['0001234#div541', '0004321#div354', '0112233#div763', ...]

请注意,在这种情况下,您应该用零填充数字字符串,以便按字符串排序是正确的。

之后,您可以简单地在数组上使用 javascript .sort() 方法。数组排序后,您可以通过解析分隔符之前的数据来获取 ID。

如果希望按降序排序,只需在排序后使用.reverse()即可。

编辑

您可以将精心制作的数组转换为所需的 ID 数组,如下所示:

var idsArray = $.map( craftedArray, function(val, i) {
  return val.substring(val.indexOf('#'));
});

An interesting method to sort would be to create an array of crafted strings that contain the sorted attribute value, a sperarator and the element's ID.

In your example data the string array should look like:

['Name One#div541', 'Name Two#div354', 'Name Three#div763', ...]

or if you want to sort by the numeric value:

['0001234#div541', '0004321#div354', '0112233#div763', ...]

Note that in that case you should pad the number strings with zeros so the sort by string will be correct.

After that you can simply use the javascript .sort() method on the array. After the array is sorted you can take the IDs by parsing out the data before the delimiter.

If you want the sorting to be in descending order, just use .reverse() after sorting.

Edit

You can convert the crafted array to your desired IDs array like this:

var idsArray = $.map( craftedArray, function(val, i) {
  return val.substring(val.indexOf('#'));
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文