jquery tablesorter and filesizes
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开发者_Python百科="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
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:
new RegExp(/[0-9]+(\.[0-9]+)?\ (KB|B|GB|MB|TB)/i)
new RegExp(/^[0-9]+(\.[0-9]+)?/i)
Maybe the regex should look like this?
s.match(new RegExp(/.*(KB|B|GB|MB|TB)$/))
Since you want to ignore the number part there...
精彩评论