views:

659

answers:

4

Hello there,

I have a table, and in each cell I want to place strings, but they are much wider than the cell width. To prevent line break, I would like to shorten the strings to fit the cell, and append '...' at end to indicate that the string is much longer.

The table has about 40 rows and has to be done to each cell, so its important that its a quick. Should I use JS/jQuery for this?

How would I do it?

Thank you for your time.

Kind regards,
Marius

+2  A: 

Here is a good SO post on getting text width. Check it out: http://stackoverflow.com/questions/1582534/calculating-text-width-with-jquery

Dustin Laine
+1  A: 

this should do the trick where 8 is is the the size of the text you want to keep

 $('td').each(function(){
    $(this).text($(this).text().substring(0,8)+"...");
 });

to answer you comment I think jquery width() should help get you going but I think your headed up a slippery slop you should consider something different like putting the text in a div or using flexigrid or jgrid

mcgrailm
How do I find calculate how wide each letter is to determine substring second argument?Thanks
Marius
+2  A: 

You can use CSS -- text-overflow: ellipsis -- for this - with some caveats for certain browsers and workarounds listed here:

http://mattsnider.com/css/css-string-truncation-with-ellipsis/

James Westgate