繁体   English   中英

使用 jQuery table-sorter 插件对日期列进行排序的问题

[英]Issue with sorting date columns with the jQuery table-sorter plugin

我为我的表实现了 jQuery TableSorter 插件,一切正常,除了包含以下格式的特定列: HH:mm, dd.MM ,例如09:45, 15.1115:48, 16.11

正如我之前使用此插件所做的那样,我尝试在 javascript 文件中对表格进行排序:

$(function() 
    $(".my-table").tablesorter({
      sortList: [[1,0]], // sorting on the second column
      dateFormat : "HH:mm, dd.MM"
    });
  });

但是,它仅按HH:mm排序,这会导致输入错误(因为它忽略了日期)。 是否有另一个特定的日期字符串可以使用它,因为我无法真正更改它,或者有没有办法编写我自己的自定义解析器并使用插件实现它? 谢谢!

dateFormat选项只允许 3 种设置: "mmddyyyy" (默认)、 "ddmmyyyy""yyyymmdd"

在这种情况下,您需要添加自定义列解析器- 请参阅此演示,但它只有 Sugar.js 库的示例。

特别是,请确保加载和使用包含 Sugar.js 和 Date.js 库的接口的解析器

如果有人偶然发现这种特定的日期格式,我会找到一个解决方案。 事实证明,TableSorter 插件允许添加自定义解析器,您可以满足您的需求。 更多关于这里的信息 - https://mottie.github.io/tablesorter/docs/example-parsers.html

在这种特定情况下,它看起来像这样,现在可以工作并且该列是可排序的:

$.tablesorter.addParser({
        id: 'custom_date_parser',
        // Auto-detection
        is: function(date) {
            return false;
        },
        // Normalize data and allow sorting for this specific type
        format: function(date) {
            // get current year
            var current_year = new Date().getFullYear()

            // obtain date values from column
            // current format HH:mm, dd.MM
            var hour = date.slice(0, 2);
            var minutes = date.slice(3, 5);
            var day = date.slice(7, 9);
            var month = date.slice(10, 12);

            // convert to date object
            return date = new Date(current_year, month - 1, day, hour - 1, minutes)
        },
        parsed: false,
        // Type of sorting to use
        type: 'numeric'
    });

// Perform the sorting
    $("#table-name").tablesorter({
        // apply custom parser only to the second column
        headers: {
            1: {
                sorter: 'custom_date_parser'
            }
        }
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM