[英]How to check length of lines inside a td for text and show more content on click
I am trying to show "see more"
which when clicked
will show the full content inside a td
. 我正在尝试显示
"see more"
,当clicked
它时将显示td
的全部内容。 The condition for see more
is that it will appear when the text inside td
has more than 3 lines, then show "see more" and onclick
expand and show full content. see more
内容的条件是,当td
的文本超过3行时,它将显示出来,然后显示“查看更多”,然后onclick
展开并显示完整内容。
Here, width is set to 318px and user may or may not change. 此处,宽度设置为318像素,用户可能会更改,也可能不会更改。
I can give line-height to td to be 21px
, and based on calculations I can calculate number of lines but in array the text are random and array size is increasing with scroll. 我可以将td的line-height设为
21px
,并且基于计算,我可以计算行数,但是在数组中,文本是随机的,并且数组大小随滚动而增加。 The array size may be in 1000s or much more. 阵列大小可能在1000s或更多。 So I don't want to maintain array size which have 1000 or more elements which contain status for each text (array elements) about number of lines.
因此,我不想保持包含1000个或更多元素的数组大小,这些元素包含有关行数的每个文本(数组元素)的状态。
Is there, any better way where a simple trick can be applied to all text (array elements). 有没有更好的方法可以将简单技巧应用于所有文本(数组元素)。
here is stackblitz link:- https://stackblitz.com/edit/angular7-test-mndgc5?file=app%2Fapp.component.html 这是stackblitz链接:-https://stackblitz.com/edit/angular7-test-mndgc5?file = app%2Fapp.component.html
// textss is dyanamic and can be 100 or with a scroll
textsss = [
"In 2005, Nature published a peer review comparing 42 science articles from Encyclopædia Britannica and Wikipedia and found that Wikipedia's level of accuracy approached that of Britannica. Time magazine stated that the open-door policy of allowing anyone to edit had made Wikipedia the biggest and possibly the best encyclopedia in the world, and was a testament to the vision of Jimmy Wales.",
"005, Nature published a peer review comparing 42 science articles from Encyclopædia Britannica and Wikipedia and found that Wikipedia's level of accuracy approached that of Britannica.",
"abcdffff",
"ished a peer review comparing 42 science articles from Encyclopædia Britannica and Wiki",
.
.
.
.
]
ngAfterViewInit(){
this.viewHeight = this.elementView.nativeElement.offsetHeight;
console.log( this.viewHeight);
this.lines = Math.round(this.viewHeight/ 21);
console.log(this.lines);
if(this.lines > 3){
this.dynamicTextHeight = 3 * 21;
}
}
expand(){
this.dynamicTextHeight = this.lines * 21;
}
<table class="table">
<tbody >
<tr *ngFor="let text of textsss;let i = index;" [style.height.px]="dynamicTextHeight">
<td #mainScreen>{{i + 1}}. <span *ngIf="lines > 3" (click)="expand()">... see more</span>{{text}}</td>
</tr>
</tbody>
</table>
Update: 更新:
Since CSS was not enough, I've refactored to handle each box as a component and made the height calculation at the beginning (AfterViewInit) and use it to display or not the extra lines. 由于CSS还不够,因此我将每个框作为一个组件进行了重构,并在开始时进行了高度计算(AfterViewInit),并使用它来显示是否显示多余的行。
Demo: https://stackblitz.com/edit/angular7-test-x7pqvp 演示: https : //stackblitz.com/edit/angular7-test-x7pqvp
You can achieve that with CSS + toggling the ngStyle: 您可以通过CSS +切换ngStyle来实现:
text-overflow
+ fixed height (calculated from font-size
, line-height
and number of lines to show). text-overflow
+固定高度(根据font-size
, line-height
和要显示的行数计算)。 And SASS may help with that calculation.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.