【问题标题】:Tablesorter + Datepicker Date Format BrokenTablesorter + Datepicker 日期格式损坏
【发布时间】:2013-09-10 17:54:05
【问题描述】:

在 tablesorterk 中使用 datepicker 时,例如 This Example 它会说:

// add any of the jQuery UI Datepicker options here (http://api.jqueryui.com/datepicker/)

我们可以假设其中包括 dateFormat,但由于某种原因,唯一有效的 dateFormat 是示例中的默认格式。

作品

dateFormat : 'M dd, yy'
// comparison: Oct 13, 2013

dateFormat : 'M dd, yy'
// comparison Sep 22, 2013

不起作用

dateFormat : 'D M dd'
// comparison: Fri Oct 04

dateFormat : 'M dd'
// comparison Sep 22

例子:

JQuery

$(function() {
  $("table").tablesorter({
    widthFixed : true,

    widgets: ["filter"],
    widgetOptions : {
      filter_formatter : {
        0 : function($cell, indx){
          return $.tablesorter.filterFormatter.uiDateCompare( $cell, indx, {
            dateFormat : 'D, M dd, yy',
            changeMonth : true,
            changeYear : true,
            compare : '='
          });
        }
      }
    }
  });
});

HTML

<table class="tablesorter">
  <thead>
    <tr>
      <th data-placeholder="Sort By Date">Date (one input; greater than)</th>
    </tr>
  </thead>
  <tbody>
    <tr><td>Wed, Jun 26, 2013</td></tr>
    <tr><td>Wed, Aug 21, 2013</td></tr>
    <tr><td>Sun, Oct 13, 2013</td></tr>
    <tr><td>Sat, Jul 6, 2013</td></tr>
    <tr><td>Tue, Dec 10, 2012</td></tr>
  </tbody>
</table>

这是对日期格式的微小更改,但会导致表格无法过滤。是否需要不同的格式?我错过了图书馆吗?

<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.0/themes/cupertino/jquery-ui.css">
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.0/jquery-ui.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
<script src="jquery.tablesorter.min.js"></script>
<script src="jquery.metadata.js"></script>
<script src="jquery.tablesorter.widgets.js"></script>
<script src="jquery.tablesorter.widgets-filter-formatter.js"></script>

【问题讨论】:

    标签: jquery datepicker jquery-ui-datepicker tablesorter


    【解决方案1】:

    此问题可能是由于插件未将该列检测为日期列。排序器最终对重新格式化的日期列使用默认文本解析器,因此排序将是错误的。

    当过滤器比较日期时,它会查找该列的已解析数据并将其与输入的过滤器(也已解析)进行比较。

    我没有制作完整的demo for you,但请尝试包含此解析器,看看它是否适合您。

    $.tablesorter.addParser({
        id: "date-ignore-weekday",
        is: function (s) {
            return false;
        },
        format: function (s, table) {
            // probably not the best regex, but it works
            var date = s.match(/\w+,\s+(.*)/);
            return date ? new Date(date[1] || '').getTime() : s;
        },
        type: "numeric"
    });
    
    $('table').tablesorter({
        theme: 'blackice',
        headers: {
            0: {
                sorter: "date-ignore-weekday"
            }
        }
    });
    

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题,我找到了这个解决方案。

      我的日期格式是 ddmmyyyy,例如 24/01/2014(一月)

      这适用于 jquery.tablesorter.widgets-filter-formatter.js

      过滤器小部件格式化程序功能 - 2013 年 11 月 9 日更新 (v2.13.3)

      第 363 行:添加此行 dateFormat: 'dd/mm/yy' // set the default date format

      所以代码现在是

      等等。无需更改此文件中的任何其他内容。

      在哪里设置表格排序器参数添加

      dateFormat : "ddmmyyyy", // set the default date format

      等等。

      享受吧。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-11
        • 2015-03-13
        • 2012-04-01
        • 2022-01-11
        • 1970-01-01
        • 2015-06-11
        相关资源
        最近更新 更多