Thursday, October 2, 2008

[jQuery] Re: search and replace TD innards

Thanks!

Yes, I get confused sometimes on what $(this) really is sometimes.

Here's the final code. Works great...

PS this is for the TableSorter plugin, which hates commas:

$(document).ready(function() {

$("table").tablesorter({headers: { 3: { sorter: "digit" },4:
{ sorter: "digit" },5: { sorter: "digit" },6: { sorter: "digit" } ,
7: { sorter: "digit" },8: { sorter: "digit" },9: { sorter:
"digit" },10: { sorter: "digit" },11: { sorter: "digit" },12:
{ sorter: "digit" }} , widgets: ['zebra'] });
$(".commas").addCommas();
}
);

jQuery.fn.addCommas = function() {
this.each(function(){
nStr = $(this).html() + '';
x = nStr.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
$(this).html(x1 + x2);
});
}

No comments: