适用于自定义日期格式的Tablesorter插件的自定义分析器

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

我无法绕过如何做到这一点。 我尝试调整此处的解析器: http : //beausmith.com/blog/custom-date-sorting-for-jquery-tablesorter-plugin/ 。 但我迷失了前注册。 为了方便帮助,我将在下面发布他的代码。

// 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标记:

 
COMPANYDESCRIPTIONINDUSTRYEXIT DATE
Cartera Commerce, Inc. Provides technology-enabled marketing and loyalty solutions Financials Feb 2010
Critical Information Network, LLC Operates library of industrial professional training and certification materials Education Apr 2011
Cynergydata Provides merchant payment processing services and related software products Merchant Processing May 2011
EVCI Career Colleges Holding Corp Operates post-secondary schools Education Jul 2012
Groundlink, Inc. Provides ground transportation services domestically and internationally Transportation Feb 2012
Haggen, Inc. Operates chain of high-end grocery stores in the Pacific Northwest Grocery Aug 2011

然后我正在使用的脚本,这是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月2月2010年4月2011年5月2011年2月2012年7月2012年

我修改了@ SamTyson的答案:

有三件事发生了变化:

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

所以,我最终得到了这个解析器代码和这个演示 。

 $.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' } } }); 

更新:添加了一行以修剪额外的空格。

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

 $.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' } } }); }); 

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