[英]Apply ellipsis in table column with different width according to the data in specific column
I want to apply ellipsis in columns with different widths according to the data that is going in a specific column eg some columns contain 25 characters and some contain 500 characters. 我想根据要在特定列中输入的数据在不同宽度的列中使用省略号,例如,某些列包含25个字符,而另一些包含500个字符。
I want a solution using javascript, because this time it contains a 7 column grid, but sometimes it may contain 4,5 or 6 columns. 我想要一个使用javascript的解决方案,因为这一次它包含7列网格,但是有时它可能包含4,5或6列。
Note: Table should be responsive. 注意:表应具有响应性。
A nice JS solution is provided in the answer by KooiInc in this question . KooiInc在此问题的答案中提供了一个不错的JS解决方案。
Simple truncation: 简单截断:
String.prototype.trunc = String.prototype.trunc ||
function(n){
return (this.length > n) ? this.substr(0,n-1)+'…' : this;
};
Usage: 用法:
var s = 'not very long';
s.trunc(5); //=> not ...
Or last word boundary: 或最后一个字的边界:
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_;
};
Usage: 用法:
var s = 'not very long';
s.trunc(11,true) //=>not very...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.