Thursday, April 30, 2009

[jQuery] Re: tablesorter.js vs Numerals with Commas

There are two simple ways of fixing this: having a hidden span or similar element appearing within your td prior to the value or better to define your own parser.

e.g.
  $.tablesorter.addParser({
    id: "commaNum",
    is: function(s) {
      return /^[\d-]?[\d,]*(\.\d+)?$/.test(s);
      },
    format: function(s) {
      return s.replace(/,/g,'');
      },
    type: 'numeric'
    });

aquaone
(yes, you could use a more precise regex...)


On Wed, Apr 29, 2009 at 20:41, David Blomstrom <david.blomstrom@gmail.com> wrote:
I'm using jQuery's tablesorter.js to create tables with sortable rows. It works fine on both text and numerals - but only if they have no commas. For example, the following column would sort properly:
2
18
401
3
15
But this column...
1,200
408
26,048
...would sort like this:
1,200
26,048
408
Does anyone know how to fix this?
I'm using PHP and MySQL to derive my data from a database table, using the following code:
$Area = number_format($row["Area"]);
Then I simply insert $Area in a dynamic table cell, like so...
<td>$Area</td>
I posted my JavaScript links below. Thanks for any tips!
* * * * *
<script src="http://MySite/js/jquery-1.3.1.min.js" type="text/javascript"></script>
<script src="http://MySite/js/tablesorter/jquery.tablesorter.js" type="text/javascript"></script>
<script language="JavaScript" type="text/JavaScript">
 $(document).ready(function() 
  { 
  $("#myTable").tablesorter({ widgets: ['zebra']} );

$("#triggerMS").click(function(){
 $("#menuMS").show();
 return false;
});
$("#menuMS").click( function(){
 $("#menuMS").hide();
 return true;
});

$("#triggerReg").click(function(){
 $("#menuReg").show();
 return false;
});
$("#menuReg").click( function(){
 $("#menuReg").hide();
 return true;
});

$("#triggerKids").click(function(){
 $("#menuKids").show();
 return false;
});
$("#menuKids").click( function(){
 $("#menuKids").hide();
 return true;
});

$("#triggerLinks").click(function(){
 $("#menuLinks").show();
 return false;
});
$("#menuLinks").click( function(){
 $("#menuLinks").hide();
 return true;
});

$("#triggerBooks").click(function(){
 $("#menuBooks").show();
 return false;
});
$("#menuBooks").click( function(){
 $("#menuBooks").hide();
 return true;
});

  }
 );
</script>

--
David Blomstrom
Writer & Web Designer (Mac, M$ & Linux)
www.geobop.org

No comments: