繁体   English   中英

根据特定列中的数据在表列中使用省略号,并使用不同的宽度

[英]Apply ellipsis in table column with different width according to the data in specific column

我想根据要在特定列中输入的数据在不同宽度的列中使用省略号,例如,某些列包含25个字符,而另一些包含500个字符。

我想要一个使用javascript的解决方案,因为这一次它包含7列网格,但是有时它可能包含4,5或6列。

在此处输入图片说明

注意:表应具有响应性。

您只能使用CSS

td{
max-width: 60px; // change as per requirement
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;;
}

在这里查看演示

KooiInc在此问题的答案中提供了一个不错的JS解决方案。

简单截断:

String.prototype.trunc = String.prototype.trunc ||
      function(n){
          return (this.length > n) ? this.substr(0,n-1)+'…' : this;
      };

用法:

var s = 'not very long';
s.trunc(5); //=> not ...

或最后一个字的边界:

String.prototype.trunc =
     function( n, useWordBoundary ){
         var isTooLong = this.length > n,
             s_ = isTooLong ? this.substr(0,n-1) : this;
         s_ = (useWordBoundary && isTooLong) ? s_.substr(0,s_.lastIndexOf(' ')) : s_;
         return  isTooLong ? s_ + '…' : s_;
      };

用法:

var s = 'not very long';
s.trunc(11,true) //=>not very...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM