jquery 表排序器和文件大小
我目前正在尝试实现 jquery tablesorter,我的列之一是文件大小,我找到了一段代码,但是我不断收到错误。
如果有人能帮助我那就太好了。
jQuery TableSorter: http://tablesorter.com/
jQuery TableSorter 文件大小代码: http://groups.google.com /group/jquery-en/browse_thread/thread/fd5729a7175f6710?fwc=1
我的表:
<table cellspacing="0" cellpadding="0" class="tablesorter">
<thead>
<tr>
<th valign="middle" align="left" class="filebox header"><input type="checkbox" id="checkboxAll" name="checkboxAll"></th>
<th valign="middle" align="left" class="header filename"><strong>Filename</strong></th>
<th valign="middle" align="left" class="header filesize"><strong>Size</strong></th>
<th valign="middle" align="left" class="header filedate"><strong>Date</strong></th>
</tr>
</thead>
<tbody class="file">
<tr>
<td valign="middle" align="left" class="filebox"><input type="checkbox" id="checkbox[]" name="checkbox[]"></td>
<td valign="middle" align="left" class="filename">002.jpg</td>
<td valign="middle" align="left" class="filesize">113 kb</td>
<td valign="middle" align="left" class="filedate">21/10/2010 12:18</td>
</tr>
<tr>
<td valign="middle" align="left" class="filebox"><input type="checkbox" id="checkbox[]" name="checkbox[]"></td>
<td valign="middle" align="left" class="filename">kasper.jpg</td>
<td valign="middle" align="left" class="filesize">41 kb</td>
<td valign="middle" align="left" class="filedate">05/07/2010 11:33</td>
</tr>
<tr>
<td valign="middle" align="left" class="filebox"><input type="checkbox" id="checkbox[]" name="checkbox[]"></td>
<td valign="middle" align="left" class="filename">Linda__Angela.jpg</td>
<td valign="middle" align="left" class="filesize">212 kb</td>
<td valign="middle" align="left" class="filedate">05/07/2010 11:46</td>
</tr>
<tr>
<td valign="middle" align="left" class="filebox"><input type="checkbox" id="checkbox[]" name="checkbox[]"></td>
<td valign="middle" align="left" class="filename">loader.gif</td>
<td valign="middle" align="left" class="filesize">5 kb</td>
<td valign="middle" align="left" class="filedate">05/07/2010 11:33</td>
</tr>
<tr>
<td valign="middle" align="left" class="filebox"><input type="checkbox" id="checkbox[]" name="checkbox[]"></td>
<td valign="middle" align="left" class="filename">Lucas.jpg</td>
<td valign="middle" align="left" class="filesize">111 kb</td>
<td valign="middle" align="left" class="filedate">05/07/2010 11:46</td>
</tr>
<tr>
<td valign="middle" align="left" class="filebox"><input type="checkbox" id="checkbox[]" name="checkbox[]"></td>
<td valign="middle" align="left" class="filename">people.psd</td>
<td valign="middle" align="left" class="filesize">2 mb</td>
<td valign="middle" align="left" class="filedate">05/07/2010 11:33</td>
</tr>
<tr>
<td valign="middle" align="left" class="filebox"><input type="checkbox" id="checkbox[]" name="checkbox[]"></td>
<td valign="middle" align="left" class="filename">people1.jpg</td>
<td valign="middle" align="left" class="filesize">16 kb</td>
<td valign="middle" align="left" class="filedate">05/07/2010 11:33</td>
</tr>
<tr>
<td valign="middle" align="left" class="filebox"><input type="checkbox" id="checkbox[]" name="checkbox[]"></td>
<td valign="middle" align="left" class="filename">P_750g_Basmati_FCollection.zip</td>
<td valign="middle" align="left" class="filesize">265 mb</td>
<td valign="middle" align="left" class="filedate">05/07/2010 11:46</td>
</tr>
<tr>
<td valign="middle" align="left" class="filebox"><input type="checkbox" id="checkbox[]" name="checkbox[]"></td>
<td valign="middle" align="left" class="filename">T_2kg_LongWhite_FCollection.zip</td>
<td valign="middle" align="left" class="filesize">441 mb</td>
<td valign="middle" align="left" class="filedate">05/07/2010 11:50</td>
</tr>
</tbody>
</table>
我的 jQuery 代码:
jQuery(document).ready( function() {
jQuery.tablesorter.addParser({
id: 'filesize',
is: function(s) {
return s.match(new RegExp( /[0-9]+(\.[0-9]+)?\ (KB|B|GB|MB|TB)/ ));
},
format: function(s) {
var suf = s.match(new RegExp( /(KB|B|GB|MB|TB)$/ ))[1];
var num = parseFloat(s.match( new RegExp( /^[0-9]+(\.[0-9]+)?/ ))[0]);
switch(suf) {
case 'B':
return num;
case 'KB':
return num * 1024;
case 'MB':
return num * 1024 * 1024;
case 'GB':
return num * 1024 * 1024 * 1024;
case 'TB':
return num * 1024 * 1024 * 1024 * 1024;
}
},
type: 'numeric'
});
jQuery(".tablesorter").tablesorter({
headers: {
2: { sorter: 'filesize' }
}
});
});
错误:
s.match(new RegExp(/(KB|B|GB|MB|TB)$/)) 为空
I'm currently trying to implement jquery tablesorter and one of my columns is filesizes, I've found a snippet of code for this, however I keep getting errors.
If anyone could help me out that'd be great.
jQuery TableSorter: http://tablesorter.com/
jQuery TableSorter Filesize code: http://groups.google.com/group/jquery-en/browse_thread/thread/fd5729a7175f6710?fwc=1
My Table:
<table cellspacing="0" cellpadding="0" class="tablesorter">
<thead>
<tr>
<th valign="middle" align="left" class="filebox header"><input type="checkbox" id="checkboxAll" name="checkboxAll"></th>
<th valign="middle" align="left" class="header filename"><strong>Filename</strong></th>
<th valign="middle" align="left" class="header filesize"><strong>Size</strong></th>
<th valign="middle" align="left" class="header filedate"><strong>Date</strong></th>
</tr>
</thead>
<tbody class="file">
<tr>
<td valign="middle" align="left" class="filebox"><input type="checkbox" id="checkbox[]" name="checkbox[]"></td>
<td valign="middle" align="left" class="filename">002.jpg</td>
<td valign="middle" align="left" class="filesize">113 kb</td>
<td valign="middle" align="left" class="filedate">21/10/2010 12:18</td>
</tr>
<tr>
<td valign="middle" align="left" class="filebox"><input type="checkbox" id="checkbox[]" name="checkbox[]"></td>
<td valign="middle" align="left" class="filename">kasper.jpg</td>
<td valign="middle" align="left" class="filesize">41 kb</td>
<td valign="middle" align="left" class="filedate">05/07/2010 11:33</td>
</tr>
<tr>
<td valign="middle" align="left" class="filebox"><input type="checkbox" id="checkbox[]" name="checkbox[]"></td>
<td valign="middle" align="left" class="filename">Linda__Angela.jpg</td>
<td valign="middle" align="left" class="filesize">212 kb</td>
<td valign="middle" align="left" class="filedate">05/07/2010 11:46</td>
</tr>
<tr>
<td valign="middle" align="left" class="filebox"><input type="checkbox" id="checkbox[]" name="checkbox[]"></td>
<td valign="middle" align="left" class="filename">loader.gif</td>
<td valign="middle" align="left" class="filesize">5 kb</td>
<td valign="middle" align="left" class="filedate">05/07/2010 11:33</td>
</tr>
<tr>
<td valign="middle" align="left" class="filebox"><input type="checkbox" id="checkbox[]" name="checkbox[]"></td>
<td valign="middle" align="left" class="filename">Lucas.jpg</td>
<td valign="middle" align="left" class="filesize">111 kb</td>
<td valign="middle" align="left" class="filedate">05/07/2010 11:46</td>
</tr>
<tr>
<td valign="middle" align="left" class="filebox"><input type="checkbox" id="checkbox[]" name="checkbox[]"></td>
<td valign="middle" align="left" class="filename">people.psd</td>
<td valign="middle" align="left" class="filesize">2 mb</td>
<td valign="middle" align="left" class="filedate">05/07/2010 11:33</td>
</tr>
<tr>
<td valign="middle" align="left" class="filebox"><input type="checkbox" id="checkbox[]" name="checkbox[]"></td>
<td valign="middle" align="left" class="filename">people1.jpg</td>
<td valign="middle" align="left" class="filesize">16 kb</td>
<td valign="middle" align="left" class="filedate">05/07/2010 11:33</td>
</tr>
<tr>
<td valign="middle" align="left" class="filebox"><input type="checkbox" id="checkbox[]" name="checkbox[]"></td>
<td valign="middle" align="left" class="filename">P_750g_Basmati_FCollection.zip</td>
<td valign="middle" align="left" class="filesize">265 mb</td>
<td valign="middle" align="left" class="filedate">05/07/2010 11:46</td>
</tr>
<tr>
<td valign="middle" align="left" class="filebox"><input type="checkbox" id="checkbox[]" name="checkbox[]"></td>
<td valign="middle" align="left" class="filename">T_2kg_LongWhite_FCollection.zip</td>
<td valign="middle" align="left" class="filesize">441 mb</td>
<td valign="middle" align="left" class="filedate">05/07/2010 11:50</td>
</tr>
</tbody>
</table>
My jQuery code:
jQuery(document).ready( function() {
jQuery.tablesorter.addParser({
id: 'filesize',
is: function(s) {
return s.match(new RegExp( /[0-9]+(\.[0-9]+)?\ (KB|B|GB|MB|TB)/ ));
},
format: function(s) {
var suf = s.match(new RegExp( /(KB|B|GB|MB|TB)$/ ))[1];
var num = parseFloat(s.match( new RegExp( /^[0-9]+(\.[0-9]+)?/ ))[0]);
switch(suf) {
case 'B':
return num;
case 'KB':
return num * 1024;
case 'MB':
return num * 1024 * 1024;
case 'GB':
return num * 1024 * 1024 * 1024;
case 'TB':
return num * 1024 * 1024 * 1024 * 1024;
}
},
type: 'numeric'
});
jQuery(".tablesorter").tablesorter({
headers: {
2: { sorter: 'filesize' }
}
});
});
The error:
s.match(new RegExp(/(KB|B|GB|MB|TB)$/)) is null
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的猜测是正则表达式区分大小写。正则表达式包含 B、KB、MB 等,但您的值是 b、kB、mB 等。
在正则表达式后面添加“i”以使其不区分大小写。像这样:
My guess is that the regexp is case sensitive. The regexp contains B, KB, MB etc, but your values are b, kB, mB, etc.
Add an "i" after a Regexp to make it case insensitive. Like this:
也许正则表达式应该看起来像这样?
因为你想忽略那里的数字部分......
Maybe the regex should look like this?
Since you want to ignore the number part there...