使用 jQuery 对列表项进行分组

2024-01-25

我有一个这样的列表:

<ul>
 <li><span class="date">2011 05 01</span><p>Text...</p></li>
 <li><span class="date">2011 05 01</span><p>Text...</p></li>
 <li><span class="date">2011 04 01</span><p>Text...</p></li>
 <li><span class="date">2011 04 01</span><p>Text...</p></li>
 <li><span class="date">2010 03 01</span><p>Text...</p></li>
 <li><span class="date">2010 02 01</span><p>Text...</p></li>
</ul>

我需要 jQuery 按年份和月份对项目进行分组,如下所示:

<ul>
 <li class="year">2011
  <ul>
   <li class="month>May
    <ul>
     <li class="item"><span class="date">2011 05 01</span><p>Text...</p></li>
     <li class="item"><span class="date">2011 05 01</span><p>Text...</p></li>
    </ul>
   </li>
   <li class="month>April
    <ul>
     <li class="item"><span class="date">2011 04 01</span><p>Text...</p></li>
     <li class="item"><span class="date">2011 04 01</span><p>Text...</p></li>
    </ul>
   </li>
  </ul>
 </li>
 <li class="year">2010
  <ul>
   <li class="month>March
    <ul>
     <li class="item"><span class="date">2011 03 01</span><p>Text...</p></li>
    </ul>
   </li>
   <li class="month>February
    <ul>
     <li class="item"><span class="date">2011 02 01</span><p>Text...</p></li>
    </ul>
   </li>
  </ul>
 </li>
</ul>

Thanks.


我写了一个带评论的解决方案here (jsFiddle) http://jsfiddle.net/XbQ8t/5 and here (jsBin) http://jsbin.com/tayijahala/1/edit?html,css,js,output.

(这两个链接具有相同的内容,但 jsFiddle 有时相当慢,所以你可能想转至 jsBin)

希望你会喜欢它!


HTML

<ul id="mainList">
    <li><span class="date">2011 05 01</span><p>Text...</p></li>
    <li><span class="date">2011 12 01</span><p>Text...</p></li>
    <li><span class="date">2011 05 01</span><p>Text...</p></li>
    <li><span class="date">2011 04 01</span><p>Text...</p></li>
    <li><span class="date">2011 04 01</span><p>Text...</p></li>
    <li><span class="date">2010 03 01</span><p>Text...</p></li>
    <li><span class="date">2010 02 01</span><p>Text...</p></li>
</ul>

脚本语言

// Month number to string
var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];

// Sorting the <li> by year
$("#mainList li").sort(function(a,b) {
    var yearA = $(a).children("span").text().split(" ")[0],
        yearB = $(b).children("span").text().split(" ")[0];
    return yearA < yearB;
}).appendTo($("#mainList"));

// Grouping the <li> by year
$("#mainList li").each(function() {
    var year = $(this).children("span").text().split(" ")[0];
    // If the grouping <li> doesn't exist, create it
    if ($("#mainList li.year." + year).length === 0) {
        $("#mainList").append($('<li class="year ' + year + '">' + year + '<ul></ul></li>'));
    }
    // Add the current <li> to the corresponding grouping <li>
    $(this).appendTo($("#mainList li.year." + year + " ul"));
});

// Sorting the <li> by month inside each grouping <li>
$("#mainList li.year ul").each(function() {
    $(this).children("li").sort(function(a,b) {
        var monthA = $(a).children("span").text().split(" ")[1],
            monthB = $(b).children("span").text().split(" ")[1];
        return monthA < monthB;
    }).appendTo($(this));
});

// Grouping the <li> by month inside each grouping <li>
$("#mainList li.year ul").each(function() {
    $this = $(this);
    $this.children("li").each(function() {
        var month = $(this).children("span").text().split(" ")[1];
        // If the grouping <li> doesn't exist, create it
        if ($this.find("li.month." + month).length === 0) {
            $this.append($('<li class="month ' + month + '">' + months[month-1] + '<ul></ul></li>'));
        }
        // Add the current <li> to the corresponding grouping <li>
        $(this).appendTo($this.find("li.month." + month + " ul")).addClass("item");
    });
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 jQuery 对列表项进行分组 的相关文章

随机推荐

  • 列表列,将列表转换为字符串作为新列

    我有一个包含一列列表的数据框 可以使用以下命令创建 import pandas as pd lists 1 1 2 12 6 ABC 2 1000 4 z a create test dataframe df pd DataFrame fr
  • 我可以在redmine中创建跨项目源参考吗?

    如果您有两个以某种方式连接的独立项目 如何引用其他项目的来源 为了引用您自己的项目的源代码 您可以使用 source some file 但由于我想引用另一个项目中的代码 我的想法是我可以编写如下内容 other project sourc
  • Kotlin 中的 IntArray 与 Array

    我不确定有什么区别IntArray and an Array
  • C# 中的跨线程事件处理

    我正在使用一个框架 该框架在单独的线程中运行自己的事件调度程序 该框架可能会生成一些事件 class SomeDataSource public event OnFrameworkEvent void FrameworkCallback T
  • log4net %属性不起作用

    我有一个小型测试项目 代码如下 class Program static void Main string args log4net GlobalContext Properties logFileName log txt log4net
  • 如何使用 cURL 获取 JavaScript 加载的网页内容?

    我正在尝试从网站上获取分数表 问题是该网站正在使用显然加载的 javascriptdocument ready 因此 我似乎没有成功获取表格 我得到的只是该页面顶部的按钮 关于如何解决这个问题有什么建议或想法吗 cURL 只会为您提供页面的
  • quarto_render 从 .bat / .cmd 文件中损坏

    这有点复杂 但我认为其他人也可能遇到这个问题 四开本很棒 但一直面临一个问题 其中一个功能 quarto render 无法呈现通过 bat cmd 文件访问的脚本中的文档 如果我从 RStudio IDE 运行 R 脚本 没有问题 但通过
  • Cairo 示例适用于 Python 2.7,但不适用于 Python 3

    最简单的例子 我们通过 Gtk 创建一个窗口 添加 Gtk DrawingArea 的绘图区域 并在其上通过 Cairo 绘制文本 Example usr bin env python from gi repository import G
  • 如何获取 JSON.Net 中 JsonProperty 的名称?

    我有一堂课是这样的 JsonObject MemberSerialization OptIn public class foo JsonProperty name in json public string Bar get set etc
  • 将接口传递给 ASP.NET MVC 控制器操作方法

    在我的 ASP NET MVC 应用程序中 我有一个界面 充当多个不同视图模型的模板 public interface IMyViewModel Client Client1 get set Client Client2 get set V
  • 移动分配与标准复制和交换不兼容

    测试新的移动语义 我刚刚询问了我在使用移动构造函数时遇到的问题 但正如评论中所表明的那样 问题实际上是当您使用标准的 复制和交换 习惯用法时 移动赋值 运算符和 标准赋值 运算符发生冲突 这是我正在使用的类 include
  • 如何在 IONIC 3 中使用路由

    我正在开发一个 Ionic 3 应用程序 现在我正在使用NavController用于路由和切换页面 Ex this navCtrl push DetailsPage 但我现在需要使用 Angular 路由 我发现类似的question h
  • 使用 Mongoose 更新 MongoDB 中的许多记录的正确方法是什么

    我使用 Mongoose 从 MongoDB 中提取一些记录 将它们导入到另一个系统中 然后我想将所有这些文档的状态 文档属性 设置为processed 我可以找到这个解决方案 通过id集更新多个文档 猫鼬 https stackoverf
  • 将多个文件/文件夹从 Windows 资源管理器传递到外部应用程序

    您好 有谁知道如何让 Windows 资源管理器将多个文件 文件夹传递到注册表中引用的外部应用程序 c 我当前能够使用 1 语法对单个文件 文件夹进行操作 但不确定如何让资源管理器传递多个项目 有谁知道如何做到这一点 当您在资源管理器中选择
  • 从 ISO8601 日期时间字符串中提取时区

    如何从 Java 8 中的 ISO 8601 日期字符串中提取时区 e g String timestamp1 2014 02 15T01 02 03Z String timestamp2 2017 10 27T16 22 27 605 0
  • 如何让 git-pull 默认变得详细?

    浏览 git config 变量和 git pull 文档 我没有看到默认情况下使 git pull pull 详细的方法 有人知道有办法吗 并不是所有的东西都有一个配置参数 但你基本上可以使用别名自己处理它 alias pv pull v
  • PHP 是否阻止了粘性位?

    我尝试使用 PHP 的 chmod 将权限设置为 02775 但由于某种原因 它只将 0775 添加到文件中 我认为这可能是 PHP 的 chmod 实现或其他问题 所以我尝试使用 PHP 的 passthru 函数运行 chmod 027
  • Google 登录:拒绝为目标域生成登录提示的权限(javascript Web 应用程序)

    我正在使用 Google 登录 Javascript API 即gapi signin button 在网络应用程序上 该应用程序由 gulp 服务器提供服务 绑定到0 0 0 0 当我在本地开发时 登录过程有效 但是当我在远程服务器上运行
  • FileReader API:如何同步读取文件

    我正在尝试读取使用 html 页面上的输入类型文件选择的文件 我已经实现了读取文件的功能 并且可以读取文件内容 但实际问题是正在读取文件内容异步地它允许执行脚本的其他功能 我将读取的文件内容存储在数组中 当移动到其他函数时 数组是空的 当引
  • 使用 jQuery 对列表项进行分组

    我有一个这样的列表 ul li span class date 2011 05 01 span p Text p li li span class date 2011 05 01 span p Text p li li span class