views:

602

answers:

2

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.

A: 

This is for the web? If so, why not use a simpler method like using css to hide the overflow?

Chris Pebble
The aim is to shorten the text so the last words are stil visible. For instace: A very very very very long text --> A very ... long text. If this can be accomplished with CSS please provide the info.
Drejc
+2  A: 

Wrap your text in the cell in a DIV. This will tell you whether the text inside the DIV is larger than the cell:

<div id='test' style='width:200px;height:100px;overflow:hidden'>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Suspendisse at felis. Etiam ullamcorper. Aenean fringilla, eros eu dapibus tristique, erat lacus vestibulum metus, nec pharetra leo ante et quam. Nunc ac mi molestie justo placerat laoreet. Morbi eget dolor. Curabitur pretium, mi quis iaculis molestie, dolor ligula sagittis orci, at sodales quam dolor quis sem. Suspendisse vitae risus. Maecenas vestibulum dolor vel augue. Sed purus. Ut nisi massa, vestibulum id, lobortis eget, aliquet eu, enim. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos.</p>
</div>
<script type="text/javascript">
alert($('test').scrollHeight)
</script>

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:

<div id='test' style='width:300px;height:100px;overflow:hidden'>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Suspendisse at felis. Etiam ullamcorper. Aenean fringilla, eros eu dapibus tristique, erat lacus vestibulum metus, nec pharetra leo ante et quam. Nunc ac mi molestie justo placerat laoreet. Morbi eget dolor. Curabitur pretium, mi quis iaculis molestie, dolor ligula sagittis orci, at sodales quam dolor quis sem. Suspendisse vitae risus. Maecenas vestibulum dolor vel augue. Sed purus. Ut nisi massa, vestibulum id, lobortis eget, aliquet eu, enim. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos.
</div>
<script type="text/javascript">
function shorten(element) { 
    if($(element).scrollHeight>$(element).getHeight()) {
     var myText = $(element).innerHTML.split(" ")
     myText.length=myText.length-2
     $(element).update(myText.join(" ")+" ...")
     window.setTimeout('shorten("'+element+'")',1)
    }

}
shorten('test')
</script>

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.

Diodeus
I like this answer very much. (Missing the closing script tag in your code sample.)
Benry
Got it. Thank you.
Diodeus
Will certainly try it out, not quite sure how It would look in "GWT" java code for a label element for instance.
Drejc
Yeah. It has to happen on the client after the page has rendered, so I'm not sure what the server-side code would look like.
Diodeus
The server usually don't know what size the browser will render the text in, since it depends on what fonts the browser has and the settings the user has.
some