How can I control the number of lines displayed on an HTML data input unit?

I have a Rails application that allows users to enter HTML text (P, OL, UL, BLOCKQUOTE). Now I want to display a brief summary of this text, but I want to make sure that I only show <= 4 "lines". I can try to shorten the text by the number of words displayed, but it can end with many lines if there are single-word LI elements, etc. How can i do this?

+5
source share
6 answers

If you understand correctly that you want to show some kind of HTML preview of the text that the user enters into the text field, for example, a preview that appears below the text field here in stackoverflow.

Probably the easiest way is CSS.

#preview { height: 4em; overflow: hidden; }

This should work if the line height is normal. This, of course, just hides the extra text. If hiding is unacceptable and you only need to generate 4 lines from Ruby to display, then this is harder. I do not know Ruby, so I can not give code examples. First, I would divide the text anywhere, where would be forced to a new line, such as <br>, <p>,<li> .. , , . , Ruby, , X, X - , . , , . , - , 4 .

, .

+1

, <br> \n? , , text.substring(0, to the location-1 where the 4th break/newline is). (, 20 ), . , , .

0

style="max-height: 40"

, .

, , .

0

, , , . , - , @.

- , div , ( CSS-), , div.

, div :

<div id="dummyDiv" style="position:absolute;top:-10000px;width:400px;font-size:smaller">
user text here
</div>

javascript (.. jQuery), , .

EDIT: flot

0
<p><%= truncate strip_tags(object.text), :length => 150, :omission => '…' %></p>

An attempt to truncate in markup requests a broken layout. I would split the text and truncate the text. If you need markup, I would use markdown or textiles to handle formatting, and then truncation based on length would be more accurate.

0
source

What about:

<input maxlength="value" />

http://www.w3schools.com/tags/att_input_maxlength.asp

-3
source

All Articles