admin管理员组文章数量:1023546
I am working with dynamic ponents, where the data being shown in the template is unknown and will vary from case to case. I have some nested mat-expansion-panels, where there is a need to display a text field for notes that have previously been stored in each expansion panel being loaded.
Since the length of the text is unknown, I want to limit the displayed note to one line, and mark that text have been overflowed with an ellipsis - followed by a "Show more" / "Show less" button. I have the show more/less functionality working, but I only want this button to be visible if the text exceeds one line, and be hidden if the note i.e., only contains three words.
I have seen JQuery often being used to acplish around similar issues, but being that I'm using Angular, JQuery is not a suitable fit.
Template:
<div class="note-field">
<span class="note">Note: </span>
<span class="note-text" [class.show]="show">{{ data.note }}</span>
</div>
<button class="show-button" (click)="show = !show">{{ show ? 'Show less': 'Show more' }}</button>
Component:
show: boolean = false;
CSS:
.note-text {
font-size: 14px;
line-height: 14px;
height: 14px;
overflow: hidden;
text-overflow: ellipsis;
}
.show {
overflow: visible;
text-overflow: none;
height: auto;
}
I've been looking at this for some days now, and one of the fixes that I unsuccessfully tried was adding the following CSS specification:
.show-button[overflow=visible] {
display: none;
}
The ellipsis also stopped working after implementing the overflow functionality.
I know it is somewhat specific, but I really need the functionality to hide and show this only if the data being loaded is over the specified limit.
I am working with dynamic ponents, where the data being shown in the template is unknown and will vary from case to case. I have some nested mat-expansion-panels, where there is a need to display a text field for notes that have previously been stored in each expansion panel being loaded.
Since the length of the text is unknown, I want to limit the displayed note to one line, and mark that text have been overflowed with an ellipsis - followed by a "Show more" / "Show less" button. I have the show more/less functionality working, but I only want this button to be visible if the text exceeds one line, and be hidden if the note i.e., only contains three words.
I have seen JQuery often being used to acplish around similar issues, but being that I'm using Angular, JQuery is not a suitable fit.
Template:
<div class="note-field">
<span class="note">Note: </span>
<span class="note-text" [class.show]="show">{{ data.note }}</span>
</div>
<button class="show-button" (click)="show = !show">{{ show ? 'Show less': 'Show more' }}</button>
Component:
show: boolean = false;
CSS:
.note-text {
font-size: 14px;
line-height: 14px;
height: 14px;
overflow: hidden;
text-overflow: ellipsis;
}
.show {
overflow: visible;
text-overflow: none;
height: auto;
}
I've been looking at this for some days now, and one of the fixes that I unsuccessfully tried was adding the following CSS specification:
.show-button[overflow=visible] {
display: none;
}
The ellipsis also stopped working after implementing the overflow functionality.
I know it is somewhat specific, but I really need the functionality to hide and show this only if the data being loaded is over the specified limit.
Share Improve this question asked Dec 16, 2021 at 16:11 binkobinko 31 silver badge2 bronze badges 01 Answer
Reset to default 4the only is create a template reference variable e.g. called content
(see the #content
in the code) and "ask" about content.scrollHeight
<span #content [class.note-text]="content.scrollHeight>15"
[class.show]="show">{{ data.note }}</span>
</div>
<button *ngIf="content.scrollHeight>15" class="show-button"
(click)="show = !show">{{ show ? 'Show less': 'Show more' }}</button>
See that if content.scrollHeigth>15, the "span" has the class "note-text" and the button is visible
a fool stackblitz
I am working with dynamic ponents, where the data being shown in the template is unknown and will vary from case to case. I have some nested mat-expansion-panels, where there is a need to display a text field for notes that have previously been stored in each expansion panel being loaded.
Since the length of the text is unknown, I want to limit the displayed note to one line, and mark that text have been overflowed with an ellipsis - followed by a "Show more" / "Show less" button. I have the show more/less functionality working, but I only want this button to be visible if the text exceeds one line, and be hidden if the note i.e., only contains three words.
I have seen JQuery often being used to acplish around similar issues, but being that I'm using Angular, JQuery is not a suitable fit.
Template:
<div class="note-field">
<span class="note">Note: </span>
<span class="note-text" [class.show]="show">{{ data.note }}</span>
</div>
<button class="show-button" (click)="show = !show">{{ show ? 'Show less': 'Show more' }}</button>
Component:
show: boolean = false;
CSS:
.note-text {
font-size: 14px;
line-height: 14px;
height: 14px;
overflow: hidden;
text-overflow: ellipsis;
}
.show {
overflow: visible;
text-overflow: none;
height: auto;
}
I've been looking at this for some days now, and one of the fixes that I unsuccessfully tried was adding the following CSS specification:
.show-button[overflow=visible] {
display: none;
}
The ellipsis also stopped working after implementing the overflow functionality.
I know it is somewhat specific, but I really need the functionality to hide and show this only if the data being loaded is over the specified limit.
I am working with dynamic ponents, where the data being shown in the template is unknown and will vary from case to case. I have some nested mat-expansion-panels, where there is a need to display a text field for notes that have previously been stored in each expansion panel being loaded.
Since the length of the text is unknown, I want to limit the displayed note to one line, and mark that text have been overflowed with an ellipsis - followed by a "Show more" / "Show less" button. I have the show more/less functionality working, but I only want this button to be visible if the text exceeds one line, and be hidden if the note i.e., only contains three words.
I have seen JQuery often being used to acplish around similar issues, but being that I'm using Angular, JQuery is not a suitable fit.
Template:
<div class="note-field">
<span class="note">Note: </span>
<span class="note-text" [class.show]="show">{{ data.note }}</span>
</div>
<button class="show-button" (click)="show = !show">{{ show ? 'Show less': 'Show more' }}</button>
Component:
show: boolean = false;
CSS:
.note-text {
font-size: 14px;
line-height: 14px;
height: 14px;
overflow: hidden;
text-overflow: ellipsis;
}
.show {
overflow: visible;
text-overflow: none;
height: auto;
}
I've been looking at this for some days now, and one of the fixes that I unsuccessfully tried was adding the following CSS specification:
.show-button[overflow=visible] {
display: none;
}
The ellipsis also stopped working after implementing the overflow functionality.
I know it is somewhat specific, but I really need the functionality to hide and show this only if the data being loaded is over the specified limit.
Share Improve this question asked Dec 16, 2021 at 16:11 binkobinko 31 silver badge2 bronze badges 01 Answer
Reset to default 4the only is create a template reference variable e.g. called content
(see the #content
in the code) and "ask" about content.scrollHeight
<span #content [class.note-text]="content.scrollHeight>15"
[class.show]="show">{{ data.note }}</span>
</div>
<button *ngIf="content.scrollHeight>15" class="show-button"
(click)="show = !show">{{ show ? 'Show less': 'Show more' }}</button>
See that if content.scrollHeigth>15, the "span" has the class "note-text" and the button is visible
a fool stackblitz
本文标签:
版权声明:本文标题:javascript - Show moreshow less text, but hide option if text does not overflow (Angular) - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745581713a2157347.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论