Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

"See more" button for text inside multiple cards built with *ngFor

I’m generating multiple cards using *ngFor and I added see more button to overflow the text inside the card

Component.html

<div *ngFor="let classifciation of tenderClassification">
                        <mat-card >
                            <mat-card-title>{{classifciation.name}} </mat-card-title>
                            <mat-card-subtitle>Starting price {{classifciation.amount}} TND</mat-card-subtitle>


                            <mat-card-actions>


                               <p [ngClass]="{'limitTextHeight': isReadMore}">
                                 {{classifciation.description}} 
                            </p>
                            <button mat-button  (click)="showText()">
                                {{ isReadMore ? 'Read More': 'Read Less' }}
                              </button>
                              
                            
                            </mat-card-actions>
                        </mat-card>
                    </div>

Component.ts

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  isReadMore = true;

  showText() {
     this.isReadMore = !this.isReadMore
  }

Component.css

.limitTextHeight {
height: 20px;
overflow: hidden;

}

And the result when I click to read more button all cards call the showText() function and all cards apply the read more but I want only that specific card to be applied with read more

Before I click
enter image description here

After I click on one read more button
enter image description here

Any solution to target that specific card?

>Solution :

Create a new component that receives a classification as input.

card.component.html


<mat-card>
    <mat-card-title>{{classifciation.name}} </mat-card-title>
    <mat-card-subtitle>Starting price {{classifciation.amount}} TND</mat-card-subtitle>


    <mat-card-actions>


      <p [ngClass]="{'limitTextHeight': isReadMore}">
        {{classifciation.description}} 
    </p>
    <button mat-button  (click)="showText()">
        {{ isReadMore ? 'Read More': 'Read Less' }}
      </button>
      
    
    </mat-card-actions>
  </mat-card>
@Component({
  selector: 'app-card',
  template: `./card.component.html`,
})
export class CardComponent  {
  @Input() classification: any;

  isReadMore = true;

  showText() {
    this.isReadMore = !this.isReadMore
  }
}

in your parent component.

<div *ngFor="let classification of tenderClassification">
  <app-card [classification]="classification"></app-card>
</div>

As you can see the code is cleaner, and you can add more behaviour to each card component.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading