Abbreviate content inside cell (HTML table)

Asked

Viewed 1,664 times

1

I have a table that is composed of values that are pulled from the database....

This table has the predefined size, and it turns out that when the value pulled from the bank is greater than the cell, it resizes and messes up everything...

Example: inserir a descrição da imagem aqui

I want to know if you can do this: inserir a descrição da imagem aqui

Code

<div class="tabela-clientes" >
   <table id="mytable">              
      <tr>
         <td style="width: 200px">
            Nome
         </td>
          <td style="width: 200px">
            Empresa
          </td>
          <td style="width: 90px">
            Telefone
          </td>
          <td style="width: 180px;">
             Email
          </td>
          <td style="width: 130px; max-width:130px;">
             Opções
          </td>
        </tr>
        ......

Someone once needed something like this?


Solution formulated with the help of Diego Souza

$nome = "{$linha['nome']}";

//conta numero de caracteres da string
$tam = strlen($nome);

//se string tiver mais que 25 caracteres
if($tam > 25){
    //Exibe apenas 25 caracteres
    $rest = substr($nome, 0, 25); 
    echo $rest . "...";
}else{
    echo $nome;
}

3 answers

1


You have to set fixed sizes for the columns. It’s the only way. In percentage or pixel.

But if you want to abbreviate the content, use a function that limits your string.

In PHP is the substr(string, inicial, quantidade).

<?php
$rest = substr("abcdef", 0, -1);  // retorna "abcde"
$rest = substr("abcdef", 2, -1);  // retorna "cde"
$rest = substr("abcdef", 4, -4);  // retorna ""
$rest = substr("abcdef", -3, -1); // retorna "de"
?>

http://php.net/manual/en/function.substr.php

  • Um, this substr method is interesting, in case I can count the number of characters of my string and if it is larger than x, then I will use the substr method

  • Ysso even! It can be so too.

  • But it will end up not working, because the string size can be big.... or muuuuito grande, then in case my substr is set to -10...it will end up not serving when the string is too large

  • Is there any way I can display only x characters of a string? Regardless of its size show up to 25 characters for example...

  • 1

    Using the substr even. substr('Essa é uma frase com 36 caracteres !', 0, 25).

  • That’s it, it worked. Thank you very much!

Show 1 more comment

1

The simplest solution is to use only CSS, in this case a combination of display, white-space, overflow and text-overflow

The Javascript function is only used to copy the text of the cell to the title property of the cell, so the user can read the text in full when passing the mouse over the cell.

var truncate = document.querySelectorAll(".truncate");
truncate = [].slice.apply(truncate);
truncate.forEach(function (elemento, indice) {
  elemento.title = elemento.innerHTML;
})
.truncate {
  display: block;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;  
}
<table id="mytable">      
  <thead>
    <tr>
      <th style="width: 200px">
        Nome
      </th>
      <th style="width: 400px">
        Descrição
      </th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td style="width: 200px">
        Toby Mosque
      </td>
      <td style="width: 400px" class="truncate" title="Hello">
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur non leo vel dui accumsan pharetra at vel dui. Integer eu volutpat quam, in ornare lectus. Aliquam elit tortor, tincidunt ac magna eget, sagittis pretium metus. Quisque non lorem tincidunt, pulvinar ligula eu, interdum augue. Nulla posuere, diam non feugiat auctor, nunc orci mollis quam, ut tempus orci purus vitae mauris. Pellentesque sodales mauris sed fringilla porta. Nulla pulvinar ipsum turpis, sed vehicula massa viverra lobortis. Integer convallis, dui condimentum tempus gravida, leo nunc mattis dolor, in lacinia velit ligula sit amet enim. Nullam varius facilisis tellus sed rutrum. Morbi auctor mi auctor dolor cursus, ut semper orci elementum. Mauris eget cursus risus. Suspendisse hendrerit luctus odio, non blandit turpis maximus at. Morbi congue augue dui, ut ultrices nisl pretium sit amet. Curabitur in nulla diam. Donec scelerisque lacinia lacinia.      </td>
    </tr>
  </tbody>
</table>

0

was researching the same subject, as I am using Angularjs, it was very easy, I will leave Aki if anyone needs, I used an angle filter, the limiteTo, then I used ng-if, for q if it has more q x characters, show "...":

 <td>{{item.t0031_razao | limitTo : 30 : 0}} <a ng-if="item.t0031_razao.length > 30">...</a> </td>

the result was thus:

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.