簡體   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