I am using the jquery tablesorter plugin to sort a table. On of my the columns in my table shows the date in mm/yy format.
我使用jquery tablesorter插件对表进行排序。我的表格中的列以mm / yy格式显示日期。
<tr>
<td class="col-name">...</td>
...
<td rel="2000" class="col-dob">10/00</td>
...
</tr>
<tr>
<td class="col-name">...</td>
...
<td rel="1986" class="col-dob">11/86</td>
...
</tr>
Note:
- Each cell has a unique class
- Date is displayed in the mm/yy format
- Cell with date receives the year as well
每个单元格都有一个独特的类
日期以mm / yy格式显示
带有日期的单元格也会收到年份
My jQuery code is as below:
我的jQuery代码如下:
// add parser through the tablesorter addParser method
$.tablesorter.addParser({
// set a unique id
id: 'user-birthdate',
is: function(s) {
// return false so this parser is not auto detected
return false;
},
format: function(s) {
// format your data for normalization
var dateSplit = s.split('/');
if(2 !== dateSplit.length)
return 0;
return new Date(dateSplit[1], dateSplit[0], 1);
},
// set type, either numeric or text
type: 'numeric'
});
myClass.init = function() {
$('.module .user table').tablesorter({
sortList: [[0,0]],
widgets: ['zebra'],
headers: {
5: {
sorter:'user-birthdate'
}
}
});
}
myClass.init();
My problem is that the tableSorter interprets 00 as year 1900 instead of 2000 and hence the sorted data is not correct.
我的问题是tableSorter将00解释为1900而不是2000,因此排序的数据不正确。
Any clue how can I resolve this? I am using jQuery 1.2.6 and the latest version of tablesorter.
任何线索如何解决这个问题?我正在使用jQuery 1.2.6和最新版本的tablesorter。
2 个解决方案
#1
The tablesorter documentation is often rather unhelpful, I've found. It looks like it says a lot, but is lacking in the details.
我发现,tablesorter文档通常是无用的。它看起来很多,但缺乏细节。
In this case, it doesn't tell you the function signature for a parser. Fortunately, you can read the unminified code to find it.
在这种情况下,它不会告诉您解析器的函数签名。幸运的是,您可以阅读未经授权的代码来查找它。
There we find that the metadata
parser does this:
在那里我们发现元数据解析器执行此操作:
format: function(s,table,cell) {
This means that you can adjust your format method to:
这意味着您可以将格式方法调整为:
format: function(s, table, cell) {
// format your data for normalization
var dateSplit = s.split('/');
var year = $(cell).attr('rel');
if(2 !== dateSplit.length)
return 0;
return new Date(year, dateSplit[0], 1);
},
Or at least similar to that. I haven't actually tested this. But it should be at least very close.
或者至少与此类似。我实际上没有测试过这个。但它应该至少非常接近。
#2
I think you will find that your problem is the Date constructor and the 2-digit year string you are passing without disambiguation: new Date(dateSplit[1], dateSplit[0], 1);
我想你会发现你的问题是Date构造函数和你没有消除歧义的2位数年份字符串:new Date(dateSplit [1],dateSplit [0],1);
I don't think you can (easily) get access to rel based on s in the parser. Does s contain the entire contents of the cell? Can you do something in the data in the cell like: <span style="display : none">CC</span>MM/YY
, strip out the tags and then combine CC with YY in your parse?
我不认为你可以(轻松)基于解析器中的s访问rel。 s包含单元格的全部内容吗?您可以在单元格中的数据中执行以下操作:
#1
The tablesorter documentation is often rather unhelpful, I've found. It looks like it says a lot, but is lacking in the details.
我发现,tablesorter文档通常是无用的。它看起来很多,但缺乏细节。
In this case, it doesn't tell you the function signature for a parser. Fortunately, you can read the unminified code to find it.
在这种情况下,它不会告诉您解析器的函数签名。幸运的是,您可以阅读未经授权的代码来查找它。
There we find that the metadata
parser does this:
在那里我们发现元数据解析器执行此操作:
format: function(s,table,cell) {
This means that you can adjust your format method to:
这意味着您可以将格式方法调整为:
format: function(s, table, cell) {
// format your data for normalization
var dateSplit = s.split('/');
var year = $(cell).attr('rel');
if(2 !== dateSplit.length)
return 0;
return new Date(year, dateSplit[0], 1);
},
Or at least similar to that. I haven't actually tested this. But it should be at least very close.
或者至少与此类似。我实际上没有测试过这个。但它应该至少非常接近。
#2
I think you will find that your problem is the Date constructor and the 2-digit year string you are passing without disambiguation: new Date(dateSplit[1], dateSplit[0], 1);
我想你会发现你的问题是Date构造函数和你没有消除歧义的2位数年份字符串:new Date(dateSplit [1],dateSplit [0],1);
I don't think you can (easily) get access to rel based on s in the parser. Does s contain the entire contents of the cell? Can you do something in the data in the cell like: <span style="display : none">CC</span>MM/YY
, strip out the tags and then combine CC with YY in your parse?
我不认为你可以(轻松)基于解析器中的s访问rel。 s包含单元格的全部内容吗?您可以在单元格中的数据中执行以下操作: