Pergunta

eu tenho um table com título curto, mas células longas (td).É possível fazer com que a largura das colunas seja a menor largura possível do título?

Exemplo:

<table>
    <tr>
        <th>Name</th>
        <th>phone</th>
    </tr>
    <tr>
        <td>Santa</td>
        <td>20938570987098709870298349082456</td>
    </tr>
</table>

Portanto, a coluna “telefone” deve mostrar “20...”

É possível?

Foi útil?

Solução

Um pouco mais difícil se você precisar ser exato na largura e não estiver usando uma fonte monoespaçada.

$("th").each(function () {
   var chars = $(this).text().length;
   $("td").eq($(this).index()).each(function () {
      if ($(this).text().length > chars) {
         $(this).text($(this).text().substring(0, chars) + '...');
      }
   });
});

Não testado ou otimizado;apenas uma prova de conceito.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top