views:

27

answers:

2

Does anyone know of a jquery plugin or snippet that will auto-text-align cells in a table based on content?

Specifically, all table cells would be right justified unless there is a visible non-number related character in the cell, then it would be left justified. I'd like to use something like this regular expression to identify non-number related characters in a cell:

/[^0-9% +-()]/ 

Is there a real simple way to accomplish this? I would think something like this:

$("td:contains('[^0-9% +-()]')").addClass("left");

would do the trick, but I don't think 'contains' can take a regular expression.

+1  A: 
  $(function(){
    $("table td").each(function(){
      if($(this).text().match(/^[0-9.%$()]+$/)){
        $(this).addClass("right");
      } else {
        $(this).addClass("left");
      }
    });
  });
andynu
adding some missing characters to your regex made this work (was missing plus and space in the charset).
ericslaw
+1  A: 

You can use the filter function:

$("td").filter(function(){return /^[0-9.%$()]+$/.test($(this).text())}).addClass("left");
edwin
I had to modify the regex here to match the original (unanchored with negated charset), as I want to left justify anything NOT number based field.This example, while more terse, does use a static regex (test rather than match) and presumes the entire table is already right justified (which coincidentally it is). The 'filter' function was a nice treat to learn.
ericslaw