【问题标题】:Custom Parser for Tablesorter plugin for custom date format用于自定义日期格式的 Tablesorter 插件的自定义解析器
【发布时间】:2012-05-05 18:59:53
【问题描述】:

我需要调整 jQuery Tablesorter 插件以非常简单的格式对日期进行排序,该格式由三个字母的月份和 4 位数字组成(例如 2010 年 5 月、2011 年 1 月、2012 年 3 月等)。

我一直不知道该怎么做。我尝试调整在这里找到的解析器:http://beausmith.com/blog/custom-date-sorting-for-jquery-tablesorter-plugin/。但是我迷失了reg ex。为了方便起见,我将在下面发布他的代码。

// TableSort parser for date format: Jan 6, 1978
$.tablesorter.addParser({
id: 'monthDayYear',
is: function(s) {
  return false;
},
format: function(s) {
  var date = s.match(/^(\w{3})[ ](\d{1,2}),[ ](\d{4})$/);
  var m = monthNames[date[1]];
  var d = String(date[2]);
  if (d.length == 1) {d = "0" + d;}
  var y = date[3];
  return '' + y + m + d;
 },
type: 'numeric'
});
var monthNames = {};
monthNames["Jan"] = "01";
monthNames["Feb"] = "02";
monthNames["Mar"] = "03";
monthNames["Apr"] = "04";
monthNames["May"] = "05";
monthNames["Jun"] = "06";
monthNames["Jul"] = "07";
monthNames["Aug"] = "08";
monthNames["Sep"] = "09";
monthNames["Oct"] = "10";
monthNames["Nov"] = "11";
monthNames["Dec"] = "12";

关于如何将其格式化为月份名称和年份的任何想法?谢谢!

更新: 我已经尝试从下面的 Sam 和 Fudgey 中实现一些代码(感谢您到目前为止提供的帮助!)。我不能让它工作。我尝试使用 fugey 的代码示例,因为我看到它在小提琴演示中完全按照需要工作。下面是我的 HTML 标记:

<table id="myTable" class="stripeMe sample" width="100%" cellpadding="0"    cellspacing="0">
<thead>
<th width="30%" align="left">COMPANY</th><th width="35%">DESCRIPTION</th><th width="17%"   align="left">INDUSTRY</th><th width="18%" align="left">EXIT DATE</th></tr></thead>
<tbody>
<tr><td width="30%">   <a href="http://www.cartera.com/vesdia.html "> Cartera Commerce,  Inc.</a>  </td>
<td width="35%">Provides technology-enabled marketing and loyalty solutions 
</td><td width="17%">   Financials  </td><td width="18%">Feb 2010</td></tr><tr><td  width="30%">   <a href="http://www.criticalinfonet.com/ "> Critical Information Network,   LLC</a>  </td>
<td width="35%">Operates library of industrial professional training and certification   materials 
</td><td width="17%">   Education  </td><td width="18%">Apr 2011</td></tr><tr><td     width="30%">   <a href="http://www.cynergydata.com/ "> Cynergydata</a>  </td>
<td width="35%">Provides merchant payment processing services and related software products 
</td><td width="17%">   Merchant Processing  </td><td width="18%">May 2011</td></tr><tr>  <td width="30%">   <a href=" "> EVCI Career Colleges Holding Corp</a>  </td>
<td width="35%">Operates post-secondary schools  
</td><td width="17%">   Education  </td><td width="18%">Jul 2012</td></tr><tr><td  width="30%">   <a href="http://www.groundlink.com/ "> Groundlink, Inc.</a>  </td>
<td width="35%">Provides ground transportation services domestically and internationally 
</td><td width="17%">   Transportation  </td><td width="18%">Feb 2012</td></tr><tr><td  width="30%">   <a href="http://www.haggen.com/ "> Haggen, Inc.</a>  </td>
<td width="35%">Operates chain of high-end grocery stores in the Pacific Northwest 
</td><td width="17%">   Grocery  </td><td width="18%">Aug 2011 </td></tr>

</tbody>
</table>

然后我正在使用的脚本是 fudgey 的,但我将列标题编号更改为 3(这是我表中的第 4 列),并且我更改了对 tablesorter 的调用以使用表的 id,其中这个案例是原来的#myTable。我还将它包装在 jQuery 的 $(document).ready 中:

$(document).ready(function() { 
$.tablesorter.addParser({
id: 'monthYear',
is: function(s) {
return false;
},
format: function(s) {
var date = s.match(/^(\w{3})[ ](\d{4})$/);
var m = date ? date[1] + ' 1 ' || '' : '';
var y = date && date[2] ? date[2] || '' : '';
return new Date(m + y).getTime() || '';
},
type: 'Numeric'
});

$('#myTable').tablesorter({
headers: {
    3: {
        sorter: 'monthYear'
    }
}
});
});

它仍然没有按日期对该列进行排序,我不确定它是如何排序的 - 我按这个顺序排序,这看起来几乎是正确的,但看看 2010 年 2 月的位置,就在中间2011 年的日期 - 奇怪: 2011 年 8 月 2010 年 2 月 2011 年 4 月 2011 年 5 月 2012 年 2 月 2012 年 7 月

【问题讨论】:

  • 有人有什么想法吗?谢谢!
  • 您需要以毫秒为单位返回日期,并且不需要monthNames数组。 See my answer to this question.
  • 感谢您回答 Vik - 但是,当我添加您的代码时,我收到以下错误:TypeError: 'function parse() { [native code] }' is not a constructor (evaluating 'new Date.parse(s)')

标签: jquery parsing tablesorter date-format


【解决方案1】:

我修改了@SamTyson 的回答:

改变了三件事:

  1. 格式函数需要能够处理空表格单元格。
  2. 格式函数必须返回字符串或数字
  3. 解析器类型只能是“数字”或“文本”。

所以,我最终得到了这个解析器代码和this demo

$.tablesorter.addParser({
    id: 'monthYear',
    is: function(s) {
        return false;
    },
    format: function(s) {
        // remove extra spacing
        s = $.trim(s.replace(/\s+/g, ' '));
        // process date
        var date = s.match(/^(\w{3})[ ](\d{4})$/),
            m = date ? date[1] + ' 1 ' || '' : '',
            y = date && date[2] ? date[2] || '' : '';
        return new Date(m + y).getTime() || '';
    },
    type: 'Numeric'
});

$('table').tablesorter({
    headers: {
        0: {
            sorter: 'monthYear'
        }
    }
});

更新:添加了一行以删除多余的空格。

【讨论】:

  • 谢谢你 fudgey,这确实是我需要的格式,我看到你的演示是如何完美运行的。我似乎无法让它在我的桌子上工作!我正在重新发布我在上面使用的代码,以便有人可能会发现我做错了什么。
  • 好的,更新 2:我已经弄清楚它为什么不起作用 - 标签内的日期文本两侧都有空格。那么我的问题是,有没有办法在解析器中解决这个问题?这些似乎是自动插入的,我使用的是 WordPress 和 Advance Custom Field 插件。该表将由最终用户管理员编辑,因此我可能还必须追踪插件插入空格的原因 - 但同时,有没有办法检查空格并忽略,或者它有完全匹配 - 谢谢!
  • 更新 3:我有它的工作 - 我能够进入我的模板文件并重新格式化 HTML/php 标记,以确保没有额外的空格被插入到表格单元格中。还有一个问题:这个客户想要他们的专栏的方式是,他们希望包括标记为“活跃”的公司,然后还有让团队兴奋的公司——那些兴奋的是我们按日期排序的公司,格式如上.我可以在日期列中放置“活动”,一旦排序,它就会在列的顶部或底部列出 - 但是,我希望的时间顺序
  • 公司被颠倒了 - 我希望首先列出活跃公司,然后是按日期排序的公司,从最近的公司开始,一直到最旧的公司在列表底部。目前,它显示活动,然后按日期从最旧到最近对公司进行排序。有没有一种相对简单的方法来解释/改变这个?感谢您迄今为止的帮助,您一直是生命的救星
  • 好的,我在尝试确定日期之前添加了一行代码来删除多余的间距。这是updated demo
【解决方案2】:

有了格式良好的日期,这应该是你的答案:

$.tablesorter.addParser({
    id: 'monthYear',
    is: function(s) {
        return false;
    },
    format: function(s) {
        var date = s.match(/^(\w{3})[ ](\d{4})$/);
        var m = date[1];
        var y = date[2];
        return new Date(m + ' ' + 1 + ' ' + y);
    },
    type: 'date'
});

$(document).ready(function() {
    $('.tablesorter').tablesorter({
      headers: {
         1: {
            sorter: 'monthYear'
         }
      }
   });
});

它使用正则表达式提取月份缩写和年份,然后将它们转换为日期进行排序。

【讨论】:

  • 感谢您花时间回答 Sam,我了解您在此处发布的内容的一般原则,但我收到以下错误:TypeError: 'null' is not an object (evaluation 'date[ 1]')。关于为什么的任何想法?谢谢!
  • 巧合的是,当我返回并尝试使用我在原始问题中输入的原始代码时,我得到了同样的错误
  • 听起来您的数据没有像正则表达式所期望的那样格式化。你能张贴一些日期栏吗?
  • 谢谢山姆,我在上面贴了一些代码,我使用的是下面的 fudgey 代码,它是你自己构建的。它仍然不会按日期对该列进行排序,而是按字母顺序进行。
  • 再次感谢山姆,我现在开始工作了,我必须删除表格单元格中的一些空格。
猜你喜欢
  • 2014-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多