1. import { Component, OnInit } from '@angular/core';
    2. import {HttpClient} from '@angular/common/http'
    3. @Component({
    4. selector: 'app-header',
    5. templateUrl: './header.component.html',
    6. styleUrls: ['./header.component.css']
    7. })
    8. export class HeaderComponent implements OnInit {
    9. public list:any
    10. public checked:boolean = true
    11. constructor(public http:HttpClient) { }
    12. ngOnInit() {
    13. var url:string = "http://yapi.demo.qunar.com/mock/36046/cart"
    14. this.http.get(url).subscribe(res=>{
    15. this.list =res
    16. console.log(this.list)
    17. })
    18. }
    19. handleChange(){
    20. this.list.forEach(item=>item.isSelected=this.checked)
    21. }
    22. onChange(){
    23. this.checked=this.list.every(item=>item.isSelected)
    24. }
    25. sub(a,b){
    26. return (a*b).toFixed(2)
    27. }
    28. handleDelete(data){
    29. var list = this.list.filter(value=>value!==data);
    30. this.list =list
    31. }
    32. sum(){
    33. var total=0;
    34. for(let i=0;i<this.list.length;i++){
    35. if(this.list[i].isSelected){
    36. total=total+this.list[i].productCount*this.list[i].productPrice
    37. }else{
    38. continue;
    39. }
    40. }
    41. return total.toFixed(2)
    42. }
    43. }
    1. <div>总价:${{sum()}}</div>
    2. <nz-table #basicTable [nzData]="dataSet">
    3. <thead>
    4. <tr>
    5. <th><label nz-checkbox [(ngModel)]="checked" (change)="handleChange($event)">全选</label></th>
    6. <th>商品</th>
    7. <th>数量</th>
    8. <th>价格</th>
    9. <th>小计</th>
    10. <th>操作</th>
    11. </tr>
    12. </thead>
    13. <tbody>
    14. <tr *ngFor="let data of list">
    15. <td>
    16. <label nz-checkbox (change)="onChange(data.isSelected)" [(ngModel)]="data.isSelected"></label>
    17. </td>
    18. <td>
    19. <img [src] ="data.productCover">
    20. <p>{{data.productName}}</p>
    21. </td>
    22. <td>
    23. <nz-input-number [(ngModel)]="data.productCount" [nzMin]="1" [nzMax]="10" [nzStep]="1"></nz-input-number>
    24. </td>
    25. <td>${{data.productPrice}}元</td>
    26. <td>
    27. ${{sub(data.productCount,data.productPrice)}}元
    28. </td>
    29. <td>
    30. <button nz-button nzType="danger" (click)="handleDelete(data)">删除</button>
    31. </td>
    32. </tr>
    33. </tbody>
    34. </nz-table>