如何在Angular中切换卡片页脚

时间:2019-01-06 15:06:38

标签: javascript html angular

有几张卡片。每张卡都有一个“查看评论”按钮。我想打开点击了“查看评论”的特定卡的页脚。现在,单击“查看评论”即可打开所有卡。

export class MenuComponent implements OnInit {

  public hideComments: boolean;

  dishes: Dish[] = [
    {
      name: 'Uthappizza',
      image: '/assets/img/Utthapizza.jpg',
      category: 'mains',
      label: 'Hot',
      price: '4.99',
      // tslint:disable-next-line:max-line-length
      description: 'A unique combination of Indian Uthappam (pancake) and Italian pizza, topped with Cerignola olives, ripe vine cherry tomatoes, Vidalia onion, Guntur chillies and Buffalo Paneer.',
      comments: [
        {
            rating: 5,
            comment: 'Imagine all the eatables, living in conFusion!',
            author: 'John Lemon',
            date: '2012-10-16T17:57:28.556094Z'
        },
        {
            rating: 4,
            comment: 'Sends anyone to heaven, I wish I could get my mother-in-law to eat it!',
            author: 'Paul McVites',
            date: '2014-09-05T17:57:28.556094Z'
        },
        {
            rating: 3,
            comment: 'Eat it, just eat it!',
            author: 'Michael Jaikishan',
            date: '2015-02-13T17:57:28.556094Z'
        },
        {
            rating: 4,
            comment: 'Ultimate, Reaching for the stars!',
            author: 'Ringo Starry',
            date: '2013-12-02T17:57:28.556094Z'
        },
        {
            rating: 2,
            comment: 'It\'s your birthday, we\'re gonna party!',
            author: '25 Cent',
            date: '2011-12-02T17:57:28.556094Z'
        }
    ]
    },
    {
      name: 'Zucchipakoda',
      image: '/assets/img/Zucchipakoda.JPG',
      category: 'appetizer',
      label: '',
      price: '1.99',
      description: 'Deep fried Zucchini coated with mildly spiced Chickpea flour batter accompanied with a sweet-tangy tamarind sauce',
      comments: [
        {
            rating: 4,
            comment: 'Imagine all the eatables, living in conFusion!',
            author: 'John Lemon',
            date: '2012-10-16T17:57:28.556094Z'
        },
        {
            rating: 5,
            comment: 'Sends anyone to heaven, I wish I could get my mother-in-law to eat it!',
            author: 'Paul McVites',
            date: '2014-09-05T17:57:28.556094Z'
        },
        {
            rating: 3,
            comment: 'Eat it, just eat it!',
            author: 'Michael Jaikishan',
            date: '2015-02-13T17:57:28.556094Z'
        },
        {
            rating: 4,
            comment: 'Ultimate, Reaching for the stars!',
            author: 'Ringo Starry',
            date: '2013-12-02T17:57:28.556094Z'
        },
        {
            rating: 1,
            comment: 'It\'s your birthday, we\'re gonna party!',
            author: '25 Cent',
            date: '2011-12-02T17:57:28.556094Z'
        }
    ]
    },
    {
      name: 'Vadonut',
      image: '/assets/img/Vadonut.jpg',
      category: 'appetizer',
      label: 'New',
      price: '1.99',
      description: 'A quintessential ConFusion experience, is it a vada or is it a donut?',
      comments: [
        {
            rating: 1,
            comment: 'Imagine all the eatables, living in conFusion!',
            author: 'John Lemon',
            date: '2012-10-16T17:57:28.556094Z'
        },
        {
            rating: 2,
            comment: 'Sends anyone to heaven, I wish I could get my mother-in-law to eat it!',
            author: 'Paul McVites',
            date: '2014-09-05T17:57:28.556094Z'
        },
        {
            rating: 3,
            comment: 'Eat it, just eat it!',
            author: 'Michael Jaikishan',
            date: '2015-02-13T17:57:28.556094Z'
        },
        {
            rating: 4,
            comment: 'Ultimate, Reaching for the stars!',
            author: 'Ringo Starry',
            date: '2013-12-02T17:57:28.556094Z'
        },
        {
            rating: 2,
            comment: 'It\'s your birthday, we\'re gonna party!',
            author: '25 Cent',
            date: '2011-12-02T17:57:28.556094Z'
        }
    ]
    },
    {
      name: 'ElaiCheese Cake',
      image: '/assets/img/ElaiCheese.jpg',
      category: 'dessert',
      label: '',
      price: '2.99',
      description: 'A delectable, semi-sweet New York Style Cheese Cake, with Graham cracker crust and spiced with Indian cardamoms',
      comments: [
        {
            rating: 5,
            comment: 'Imagine all the eatables, living in conFusion!',
            author: 'John Lemon',
            date: '2012-10-16T17:57:28.556094Z'
        },
        {
            rating: 4,
            comment: 'Sends anyone to heaven, I wish I could get my mother-in-law to eat it!',
            author: 'Paul McVites',
            date: '2014-09-05T17:57:28.556094Z'
        },
        {
            rating: 3,
            comment: 'Eat it, just eat it!',
            author: 'Michael Jaikishan',
            date: '2015-02-13T17:57:28.556094Z'
        },
        {
            rating: 4,
            comment: 'Ultimate, Reaching for the stars!',
            author: 'Ringo Starry',
            date: '2013-12-02T17:57:28.556094Z'
        },
        {
            rating: 2,
            comment: 'It\'s your birthday, we\'re gonna party!',
            author: '25 Cent',
            date: '2011-12-02T17:57:28.556094Z'
        }
    ]
    }
   ];

  public toggleComments(event){
      console.log(event.target)
    this.hideComments = !this.hideComments;
  }

  constructor() { 

  }

  ngOnInit() {
    this.hideComments = false;
   } 



}
<div class="container-fluid p-5">
  <div class="row">
    <h3 style="color: black"><span style="border-bottom: 2px solid grey">Menu</span></h3>
  </div>

  <div class="row mt-4">
    <div class="col-md-3" *ngFor="let dish of dishes">
        <div class="card shadow" style="height: 100%">
            <img src="{{dish.image}}" class="card-img-top" style="height: 300px">
            <div class="card-body">
              <div class="dish-info" style="height: 150px">
                  <h5 class="card-title">{{dish.name}}</h5>
                  <p class="card-text">{{dish.description}}</p>
              </div>
              <small class="float-right" [hidden]="!!this.hideComments" (click)="toggleComments($event)">View Comments...</small>
              <small class="float-right" [hidden]="!this.hideComments" (click)="toggleComments($event)">Close Comments...</small>
            </div>
            <div class="card-footer" [hidden]="!this.hideComments" style="height: 200px; overflow: scroll">
              <div *ngFor="let comment of dish.comments">
                  <div class="media">
                      <img src="assets/img/user.png" class="mr-2" style="height: 35px; width: 40px">
                      <div class="media-body">
                        <h6 class="mt-0">{{comment.author}}</h6>
                        <p style="font-size: 12px; color:#21252991">{{comment.comment}}</p>
                        <small class="float-left" style="color: #6b8096">{{comment.date | date}}</small>
                        <small class="float-right" style="color: #6b8096">Rating: {{comment.rating}}/5</small>
                      </div> 
                    </div>
                    <hr>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

仅单击“查看评论”按钮的特定卡。现在,当我单击“查看评论”时,所有的页脚都在打开。

1 个答案:

答案 0 :(得分:0)

因为所有卡共享相同的状态(hideComments,这是一个单一值),所以当您单击特定卡时,所有卡都将打开和关闭。如果要使卡具有自己的状态,则应使用索引dishes

...
<div class="col-md-3" *ngFor="let dish of dishes; let i=index">
...

然后,您可以在toggleComments方法中使用索引,该索引是通过单击特定的卡片操作触发的。当然hideComments应该是boolean的数组。