How to limit the characters of a label?

1

I'm in an e-commerce project, and I need to display the name of a product. I'm thinking of using a label for this. The idea is to leave x characters visible, if the title exceeds this x number of characters, display "...". I do not know how to do this, nor if the best option or if it is possible with a label.

Remarks: I am using jsp to retrieve the product title from the database. The programming language is JAVA and I use html and jsp . I can retrieve the name with <%anuncio.getTitulo()%> .

Any tips or hints on how to do this?

    
asked by anonymous 06.03.2017 / 19:58

3 answers

3

Java Servlet 3.0 or higher

If you are using Java Servlet version 3.0 or higher you can use this code snippet:

<lable><c:out value="${anuncio.getTitulo().length() <= maxSize ? anuncio.getTitulo() : anuncio.getTitulo().substring(0, maxSize).concat('...')}" /></lable>

maxSize is the maximum size you want in the label.

If you're going to use this in multiple places, creating a tagfile is a good option.

Java Servlet Below Version 3.0

Another option, if you are using a version below Java Servlet 3.0, is to use the following scriptlet :

<label>
    <%
        int maxSize = 5;
        String titulo = anuncio.getTitulo();
        out.println(titulo.length() <= maxSize ? titulo : titulo.substring(0, maxSize).concat("..."));
    %>
</label>
    
06.03.2017 / 21:10
1

You can also do this discussion using only HTML and CSS:

label{
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    word-wrap: hyphenate;
    max-width: 100px;
    display: block;
}
label > span{
  overflow: hidden;
}
<label>
  <span>
    Isso é um texte com um label muito grande
  </span>
</label>

Embrace

    
06.03.2017 / 23:44
0

I do not know how to do this in jsp, but in direct javascript in html it would look like this:

In js:

<label id="label">Estou num projeto e-commerce, e preciso exibir o nome de um produto</label>


(function(){
var element = document.getElementById("label");
element.innerHTML  = 
                        //@inicio, //@comprimento
 element.innerHTML.substr(0, 10)+"...";
})()

The code replaces the text of the element with its own "broken" text in x number of characters (including spaces) and concatenates with "..."

link

    
06.03.2017 / 20:51