I would like to shorten very long descriptions to the available table column width. I have the column width information in pixels. Now I would like to convert this measure to the number of characters, so I can shorten the text to the specified number.
I doesn't have to be 100% correct, a near assumption will also work.
The CSS property that he is talking about is "text-overflow".
Try adding the following to you element's class, all are required:
Wrap your text in the cell in a DIV. This will tell you whether the text inside the DIV is larger than the cell:
If you wanted to truncate at the end of a word, and add an ellipsis (...) you could, in script, start removing words until the height is equal to or less than the container. (I'm using Protoype for the $ shortcut)
Here is a working example:
You could do with sentences by splitting on a period, instead of a space, but you'd need a fall-back if no period was found, or if what was left over after the truncation is too short.
I am using jquery 1.4.2 and the earlier answer did not solve the problem but helped.. here is the code with some minor adjustments. remember that the container needs a fixed height and overflow hidden.
This is for the web? If so, why not use a simpler method like using css to hide the overflow?