m0_71681226 2022-11-15 19:52 采纳率: 100%
浏览 18
已结题

angular制作一个删除按钮时不管点击哪个按钮,删除的都是表格的最后一个元素。

angular制作一个删除按钮时不管点击哪个按钮,删除的都是表格的最后一个元素。
数据是在一个数组里存储的。
app.component.ts:

import { Component } from '@angular/core';
import { PatientData } from './PatientData';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'myapp';
  Patient = Patient;

del(Id: number){
 if (confirm('Are you sure delete this Item ?')) {
    const index = Patient.indexOf(Id);   
    const delEle = Patient.splice(index, 1);  
      console.log(index); 
    return Patient;            
}
 else {  
             return Patient;
        }  
 }
}
const Patient: any[]= [
    { Id: 0, FirstName: "Stark", LastName: "Bill", DateOfBirth: "2000/10/11", Gender: "Male", PrimaryInsurance: "Medicare", Address: "ddadsdasd", ContactNumber: "1338872818738", NextOfKin: "weqe" },
    { Id: 1, FirstName: "Shown", LastName: "Jenny", DateOfBirth: "2000/10/11", Gender: "Male", PrimaryInsurance: "Medicare", Address: "ddadsdasd", ContactNumber: "1338872818738", NextOfKin: "weqe" },
    { Id: 2, FirstName: "Geogrge", LastName: "Hash", DateOfBirth: "2000/10/11", Gender: "Male", PrimaryInsurance: "Medicare", Address: "ddadsdasd", ContactNumber: "1338872818738", NextOfKin: "weqe" },
    { Id: 3, FirstName: "Intwre", LastName: "zaiwoo", DateOfBirth: "2000/10/11", Gender: "Male", PrimaryInsurance: "Medicare", Address: "ddadsdasd", ContactNumber: "1338872818738", NextOfKin: "weqe" },
    { Id: 4, FirstName: "Niko", LastName: "baby", DateOfBirth: "2000/10/11", Gender: "Male", PrimaryInsurance: "Medicare", Address: "ddadsdasd", ContactNumber: "1338872818738", NextOfKin: "weqe" }
    ];
    

app.component.html:

<body><h1 class="title">patient data</h1>
<p><a routerLink="/second">client-side web</a> | <a routerLink="/third">admin-side web</a></p><router-outlet></router-outlet></body>
`<table class="list-table">
    <thead>
      <th>Patient ID*</th>
      <th>First name*</th>
      <th>Last name*</th>
      <th>Date of birth *</th>
      <th>Gender*</th>
      <th>Primary insurance*</th>
      <th>Address *</th>
      <th>Contact number *</th>
      <th>Next of kin</th>
    </thead>
    <tbody>
      <tr *ngFor="let patient of Patient">
        <td>{{ patient.Id }}</td>
        <td>{{ patient.FirstName }}</td>
        <td>{{ patient.LastName }}</td>
        <td>{{ patient.DateOfBirth }}</td>
        <td>{{ patient.Gender }}</td>
        <td>{{ patient.PrimaryInsurance }}</td>
        <td>{{ patient.Address }}</td>
        <td>{{ patient.ContactNumber }}</td>
        <td>{{ patient.NextOfKin }}</td>
        <td> <button class="button" (click)="del(patient.Id)">Remove</button></td>
         </tr>
    </tbody>
  </table>

img


无论我点击哪个Remove按钮删除的都是最下面一行元素,我想当点击Remove按钮删除那个按钮所在一行的元素。
请问这个问题该怎么解决?

  • 写回答

2条回答 默认 最新

  • CSDN专家-showbo 2022-11-15 20:31
    关注

    这里传入的是数字,del中的Patient.indexOf是对象,找不到对象返回-1,再splice(-1,1)就是最后一个

    <td> <button class="button" (click)="del(patient.Id)">Remove</button></td>
    
    

    直接传入对象

    <td> <button class="button" (click)="del(patient)">Remove</button></td>
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 11月15日
  • 已采纳回答 11月15日
  • 创建了问题 11月15日

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测