【问题标题】:Jquery Table Sorter Date Extraction with Text带有文本的 Jquery 表排序器日期提取
【发布时间】:2012-10-02 04:05:18
【问题描述】:

我正在尝试使用最新可用的 Jquery Table sorter 插件。 http://mottie.github.com/tablesorter/docs/#Demo

问题是我有一个日期格式的表格列,它还包含一个电子邮件值。我正在尝试提取日期值并根据日期值对其进行排序。

由于我使用的是最新版本的表格排序器,因此我尝试使用最新版本 (http://mottie.github.com/tablesorter/docs/example-parsers-class-name.html) 中提供的解析器类名称。

请在下面找到我的小提琴。

http://jsfiddle.net/meetravi/pztqe/8/

代码片段:

      <tr>
        <th>Name</th>
        <th>Major</th>
        <th>Sex</th>
        <th>English</th>
        <th>Japanese</th>
        <th>Calculus</th>
        <th>Geometry</th>
        <th class="sorter-shortDate">Date</th>
      </tr>
      <tbody>
      <tr>
        <td>Student01</td>
        <td>Languages</td>
        <td>male</td>
        <td>80</td>
        <td>70</td>
        <td>75</td>
        <td>80</td>
        <td><em>11/01/12 11:42</em><spanclass="label">xyz@xyz.com</span></td>
    </tr>
    </tbody>

<script type="text/javascript">
   $(document).ready(function(){
     $('#table-Id').tablesorter({
            theme: 'blue',
            dateFormat : "ddmmyy",
            textExtraction: {
                7: function(node, table, cellIndex) {
                    return $(node).find("em").text();
                }
            }
        });


});


</script>

【问题讨论】:

  • 您可以设置年/月/日格式的日期...这将是一个快速修复,但不是最好的
  • 谢谢迈克,但在我的情况下,我只能使用上述格式或美国长格式。我希望基于此进行排序。
  • 你可以先把邮箱和日期分开,这样会更好开始^^

标签: javascript jquery jquery-plugins tablesorter


【解决方案1】:

首先,跨度和类之间应该有一个空格。

其次,日期解析器仅设置为使用 4 位数年份ddmmyyyy。请参阅 this issue 以获取适用于 2 位数年份的解析器,但请阅读所有内容以了解 IE 如何处理 2 位数年份。

<td><em>11/01/2012 11:42</em><span class="label">xyz@xyz.com</span></td>

第三,由于日期列的内容,您需要在标题中设置排序选项:

headers: {
    7: { sorter: 'shortDate' }
}

最后,演示中有两个textExtraction 选项。第二个,而不是您在上面发布的那个,正在覆盖该功能。你写的那个很完美:)

Here is a demo 以上提到的变化。


更新:这是一个使用以下解析器代码的updated demo

$.tablesorter.addParser({
    id: "ddmmyy",
    is: function(s) {
        return false;
    },
    format: function(s, table, cell, cellIndex) {
        s = s
            // replace separators
            .replace(/\s+/g," ").replace(/[\-|\.|\,]/g, "/")
            // reformat dd/mm/yy to mm/dd/yy
            .replace(/(\d{1,2})[\/\s](\d{1,2})[\/\s](\d{2})/, "$2/$1/$3");
        var d = new Date(s), y = d.getFullYear();
        // if date > 50 years old, add 100 years
        // this will work when people start using "70" and mean "2070"
        if (new Date().getFullYear() - y > 50) {
            d.setFullYear( y + 100 );
        }
        return d.getTime();
    },
    type: "numeric"
});

【讨论】:

  • 非常感谢 Mottie 提供的出色工具(Tablesorter)和这个答案。
  • 查看我添加的更新演示......它仅适用于“ddmmyy”格式,但如果您希望它适用于“mmddyy”,则只需将替换顺序更改为@987654329 @;并将解析器重命名为“mmddyy”;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-04
  • 2017-07-22
  • 2016-12-11
  • 2015-03-03
  • 1970-01-01
相关资源
最近更新 更多