简体   繁体   English

如何检查td内文本的行长并在单击时显示更多内容

[英]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

.ts .TS

// 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;
  }

.html html的

<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来实现:

  1. text-overflow + fixed height (calculated from font-size , line-height and number of lines to show). text-overflow +固定高度(根据font-sizeline-height和要显示的行数计算)。 And SASS may help with that calculation. SASS可能会帮助进行该计算。
  2. Display see more/less by switching the ngStyle definition. 通过切换ngStyle定义,显示更多/更少。

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

相关问题 如何检查 a 中的值是否为 null 并在另一个中显示基于该值的文本? - How to check if the value in a <td> is null and show a text based on that in another <td>? 根据行数而不是文本长度显示更多/更少 - Show more/less based on number of lines instead of text length 如何在表格网格中隐藏/显示特定长度内的更多文本 - How to hide/show more text within a certain length in table grid jQuery在td内添加一个文本框,如果第二次在同一单元格内发生click事件,则阻止添加更多文本框 - jQuery add one text box inside a td and prevent adding more if click event occured second time inside the same cell 如何检查所选文本是否在两行文本内 - how to check if selected text is inside two lines of text 使用“显示更多”按钮显示动态内容,至少 3 行内容可见 - Show dynamic content with "Show More" button with minimum 3 lines of content visible 如何在里面插入组件<td>点击 - How to insert component inside <td> on click 如果文本长度超过 20,则显示显示更多/显示更少选项 - Display Show More / Show Less option if text if more then 20 length 您如何在表格的 tr 上仅显示特定 td 文本的第一行并在单击时展开? - How do you show just the first line of the particular td's text on a tr of a table and expand on click? 如何检查内容的长度? - how to check the content's length?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM