【问题标题】:Looking for a javascript solution to reorder divs寻找重新排序 div 的 javascript 解决方案
【发布时间】:2011-10-02 00:18:41
【问题描述】:

我在页面中有一些 div 显示相同类型的不同内容,例如优惠,现在优惠有结束时间,还有发布时间,如果用户想按结束时间或发布时间订购,他们应该重新订购。

我正在寻找可以做到这一点的 javascript 解决方案,Ext JS 或 JQuery 下的任何特定库都可以工作

这是这些 div 的样子

<div data-sortunit="1" data-sort1="40" data-sort2="156" data-sort3="1"
data-sort4="1317620220" class="item">
</div>

<div data-sortunit="2" data-sort1="30" data-sort2="116" data-sort3="5"
data-sort4="1317620220" class="item">
</div>

<div data-sortunit="3" data-sort1="10" data-sort2="157" data-sort3="2"
data-sort4="1317620220" class="item">
</div>

所以我希望能够根据 data-sortN 对这些 div 进行排序,N 是一个整数

【问题讨论】:

  • 向我们展示您的 HTML 示例。究竟如何处理这取决于您的 HTML 是如何工作的。例如,您要排序的值在哪里?是 div 的 HTML 吗?它是 div 的属性吗?
  • 添加示例,谢谢

标签: javascript jquery extjs


【解决方案1】:

编辑:好的,现在您已经提供了一些 HTML,下面是 javascript 代码,它将按所需的列号对特定的 HTML 进行排序:

function sortByDataItem(containerID, dataNum) {
    var values = [];
    $("#" + containerID + " .item").each(function(index) {
        var item = {};
        item.index = index;
        item.obj = this;
        item.value = $(this).data("sort" + dataNum);
        values.push(item);
    });
    values.sort(function(a, b) {return(b.value - a.value);});
    var container = $("#" + containerID);
    for (var i = 0; i < values.length; i++) {
        var self = $(values[i].obj);
        self.detach();
        container.prepend(self);
    }
    return;
}


$("#sort").click(function() {
    var sortValue = $("#sortColumn").val();
    if (sortValue) {
        sortValue = parseInt(sortValue, 10);
        if (sortValue && sortValue > 0 && sortValue <= 3) {
            sortByDataItem("container", sortValue);
            return;
        }
    }
    $("#msg").show(1).delay(5000).fadeOut('slow');
});

您可以在 jsFiddle 中看到它在这里工作:http://jsfiddle.net/jfriend00/JG32X/


由于您没有给我们任何 HTML,我已经制作了自己的 HTML 并向您展示了如何使用 jQuery 进行排序:

HTML:

<button id="sort">Sort</button><br>
<div id="productList">
    <div class="row"><div class="productName">Popcorn</div><div class="price">$5.00</div></div>
    <div class="row"><div class="productName">Peanuts</div><div class="price">$4.00</div></div>
    <div class="row"><div class="productName">Cookie</div><div class="price">$3.00</div></div>
    <div class="row"><div class="productName">Beer</div><div class="price">$5.50</div></div>
    <div class="row"><div class="productName">Soda</div><div class="price">$4.50</div></div>
</div>

Javascript(页面加载后运行):

$("#sort").click(function() {
    var prices = [];
    // find all prices
    $("#productList .price").each(function(index) {
        var str = $(this).text();
        var item = {};
        var matches = str.match(/\d+\.\d+/);
        if (matches && matches.length > 0) {
            // parse price and add it to the prices array
            item.price = parseFloat(matches[0]);
            item.row = $(this).closest(".row").get(0);
            item.index = index;
            prices.push(item);
        }
    });
    // now the prices array has all the prices in it
    // sort it using a custom sort function
    prices.sort(function(a, b) {
        return(a.price - b.price);
    });
    // now pull each row out and put it at the beginning
    // starting from the end of the prices list
    var productList = $("#productList");
    for (var i = prices.length - 1; i >= 0; i--) {
        var self = $(prices[i].row);
        self.detach();
        productList.prepend(self);        
    }
});

还有一个 jsFiddle 展示了它的实际效果:http://jsfiddle.net/jfriend00/vRdrA/

【讨论】:

  • 哇,非常感谢,我没想到有人会这么好心来真正完成整个工作,你是一个了不起的人。
【解决方案2】:

我根据 jfriend00 的回答做了一个小小的 jqueryPlugin:

(function($){
   $.fn.sortChildrenByDataKey = function(key, desc){
      var i, els = this.children().sort(function(a, b) {return (desc?1:-1)*($(a).data(key) - $(b).data(key));});
      for (i = 0; i < els.length; i++) {
          this.prepend($(els[i]).detach());
      }
      return this;
  };
})(jQuery);

您的 HTML:

<div id="myContainer">
  <div data-myKey="4"> ... </div>
  <div data-myKey="2"> ... </div>
  ...
</div>

用法:

$('div#myContainer').sortChildrenByDataKey('myKey', true_or_false);

容器的子元素可以是任何元素。唯一重要的是,它们是直接子级并且具有 data-X 键。

谢谢你,jfriend00!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-20
    • 2017-07-27
    • 1970-01-01
    • 1970-01-01
    • 2022-10-24
    • 2019-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多