I need to build a readmore directive in Angular2. What this directive will do is for collapse and expand long blocks of text with "Read more" and "Close" links. Not on the basis of the character count but on the basis of the specified max height.
<div read-more [maxHeight]="250px" [innerHTML]="item.details">
</div>
Can anyone please guide what would be the most reliable way to get/set the height of the element for this specific case.
Any guidelines on how this specific directive could be implemented would also be highly appreciated.
I need to build something like this https://github.com/jedfoster/Readmore.js
Solution:
With the help from Andzhik I am able to build the below component that meets my requirements.
import { Component, Input, ElementRef, AfterViewInit } from '@angular/core';
@Component({
selector: 'read-more',
template: `
<div [innerHTML]="text" [class.collapsed]="isCollapsed" [style.height]="isCollapsed ? maxHeight+'px' : 'auto'">
</div>
<a *ngIf="isCollapsable" (click)="isCollapsed =! isCollapsed">Read {{isCollapsed? 'more':'less'}}</a>
`,
styles: [`
div.collapsed {
overflow: hidden;
}
`]
})
export class ReadMoreComponent implements AfterViewInit {
//the text that need to be put in the container
@Input() text: string;
//maximum height of the container
@Input() maxHeight: number = 100;
//set these to false to get the height of the expended container
public isCollapsed: boolean = false;
public isCollapsable: boolean = false;
constructor(private elementRef: ElementRef) {
}
ngAfterViewInit() {
let currentHeight = this.elementRef.nativeElement.getElementsByTagName('div')[0].offsetHeight;
//collapsable only if the contents make container exceed the max height
if (currentHeight > this.maxHeight) {
this.isCollapsed = true;
this.isCollapsable = true;
}
}
}
Usage:
<read-more [text]="details" [maxHeight]="250"></read-more>
If there could be any improvements, please feel free to suggest.
With the help from Andzhik I am able to build the below component that meets my requirements.
Usage:
and usage
<read-more [text]="this is test text" [maxLength]="10" [showToggleButton]="true"></read-more>
If you want to display the text to the maximum number of chars without cutting any word, change this line of code:
To:
Again i solved these types of problem with dynamic data and full controlling.
Here , personalBasicModel.professionalSummary contain string . like any text.
slice:0:200 = use slice pipe for splice string with 200 character length . you can be change length according your requirement. id="dots" & id="more" two important thing.
Here we define a button with dynamic text (see more and see less ) with click event.
//---------------------------------- ts file -----------------------------------//
Define variable
showLess_More : string = "SEE MORE...";
paasValueOn_SeeMoreBtn : boolean = true;
Event(Method) fire when user click on see more button
lineheight approach:-
lineheight
and little calculation and some csstext-overflow: ellipsis;
does this job..css
.html
.ts
I think you'll need a
Component
rather thenDirective
.Components
makes more sense since you need to add Read more button/link, i.e. update DOM.Usage: