I have a table that has 100% width. It is generated dynamically with values from my db. I need to truncate TH values if they do not fit in the table, without pushing the boundaries of the cell. If the value is truncated I’d like to add a link with … and put the full length label into title tag. I don’t need it to expand. Something like this:
<th>
Long label<a href="javascript:void(0)" title="$myFullValue">...</a>
</th>
I’m not sure how to get the value of the string…
$("TH").width();
$("TH").val().width(); ??
I know there are some jQuery plugins out there but I was not able to find what I need and I thought I could try and give this project my own whirl.
EDIT:
Just an idea that crossed my mind. Couldn’t I wrap the label in tags and get it’s width. At the same time get the width of the parent TH. If labe’s width is greater, I could
- subtract, say 20 px (for …) from TH width and,
- assign that width to span’s width
- while adding overflow:hidden, and
- appending “…” to the span
Won’t this work?
This solution uses a fixed value but if you’re able to find a way to calculate the
maxLengthyou could wrap this in afunctionand pass it the output as themaxLength.FIDDLE http://jsfiddle.net/m72Ja/2/
HTML
JS