Angular 11待办应用表格编辑影响全量行与勾选删除线失效问题
问题原因
- 你定义的
isEditClicked、isCheck都是全局状态变量,所有任务行共用这两个状态,修改后自然会作用到全部行 - 任务数组
taskArray存储的是纯字符串,无法为每个任务单独保存编辑状态、完成状态 - 删除方法
deleteData误用pop,pop方法不接收参数,永远删除数组最后一项,并不是删除指定下标的任务
修复方案
修改app.component.ts
import {Component, OnInit} from '@angular/core'; // 定义任务数据结构 interface Task { content: string; isEditing: boolean; isCompleted: boolean; } @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.scss'] }) export class AppComponent implements OnInit { title = 'TO-DO-App'; // 任务数组改为存储Task对象 taskArray: Task[] = []; name = ''; isPresent:boolean = false; constructor() { } ngOnInit(): void { // 移除原来的抛出错误逻辑,否则页面初始化直接报错 // throw new Error('Method not implemented.'); } editData(task: Task, index: number): void { // 只修改当前任务的编辑状态 task.isEditing = true; } deleteData(task: Task, i: number): void { // 用splice删除指定下标的任务 this.taskArray.splice(i, 1); } completeTask(task: Task, i: number, update: string): void { task.content = update; task.isEditing = false; } addTask(task: string): void { if (!task.trim()) return; // 校验重复 const isDuplicate = this.taskArray.some(item => item.content === task.trim()); if (!isDuplicate) { this.taskArray.push({ content: task.trim(), isEditing: false, isCompleted: false }); this.name = ''; } } isChecked(task: Task, i: number, $event: any): void { // 直接切换当前任务的完成状态 task.isCompleted = $event.target.checked; } }
修改app.component.html
<div> <div class = "row" style = "margin-top:5px;margin-left: 10px;"> <div class = "col-sm-8"> <input #taskDetails [(ngModel)] = "name" class = "form-control" placeholder = "Enter task" type = "text"> </div> <div class = "col-sm-4"> <button (click) = "addTask(taskDetails.value)" class = "btn btn-primary">Add</button> </div> </div> </div> <table class = "table"> <thead> <tr> <th scope = "col">List of Task</th> <th scope = "col">Action</th> </tr> </thead> <tbody> <tr *ngFor = "let task of taskArray;index as i;"> <td> <div class = "row"> <div class = "col-xs-9"> <!-- 绑定当前任务的编辑状态和完成状态,完成则加删除线 --> <input [readOnly] = "!task.isEditing" [class.text-decoration-line-through]="task.isCompleted" class = "form-control input-data" name = "task-{{i}}" type = "text" value = "{{task.content}}" #update> </div> </div> </td> <td> <div class = "row"> <div class = "col-sm-3"> <svg (click) = "deleteData(task,i)" class = "bi bi-trash" fill = "currentColor" height = "16" viewBox = "0 0 16 16" width = "16" xmlns = "http://www.w3.org/2000/svg"> <path d = "M5.5 5.5A.5.5 0 0 1 6 6v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm2.5 0a.5.5 0 0 1 .5.5v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm3 .5a.5.5 0 0 0-1 0v6a.5.5 0 0 0 1 0V6z"/> <path d = "M14.5 3a1 1 0 0 1-1 1H13v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V4h-.5a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1H6a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1h3.5a1 1 0 0 1 1 1v1zM4.118 4 4 4.059V13a1 1 0 0 0 1 1h6a1 1 0 0 0 1-1V4.059L11.882 4H4.118zM2.5 3V2h11v1h-11z" fill-rule = "evenodd"/> </svg> </div> <!-- 只有当前任务处于编辑状态才显示保存按钮 --> <div *ngIf = "task.isEditing" class = "col-sm-3"> <svg (click) = "completeTask(task,i, update.value)" class = "bi bi-check" fill = "currentColor" height = "16" viewBox = "0 0 16 16" width = "16" xmlns = "http://www.w3.org/2000/svg"> <path d = "M10.97 4.97a.75.75 0 0 1 1.07 1.05l-3.99 4.99a.75.75 0 0 1-1.08.02L4.324 8.384a.75.75 0 1 1 1.06-1.06l2.094 2.093 3.473-4.425a.267.267 0 0 1 .02-.022z"/> </svg> </div> <!-- 非编辑状态才显示编辑按钮 --> <div class = "col-sm-3" *ngIf="!task.isEditing"> <svg (click) = "editData(task,i)" class = "bi bi-pencil" fill = "currentColor" height = "16" viewBox = "0 0 16 16" width = "16" xmlns = "http://www.w3.org/2000/svg"> <path d = "M12.146.146a.5.5 0 0 1 .708 0l3 3a.5.5 0 0 1 0 .708l-10 10a.5.5 0 0 1-.168.11l-5 2a.5.5 0 0 1-.65-.65l2-5a.5.5 0 0 1 .11-.168l10-10zM11.207 2.5 13.5 4.793 14.793 3.5 12.5 1.207 11.207 2.5zm1.586 3L10.5 3.207 4 9.707V10h.5a.5.5 0 0 1 .5.5v.5h.5a.5.5 0 0 1 .5.5v.5h.293l6.5-6.5zm-9.761 5.175-.106.106-1.528 3.821 3.821-1.528.106-.106A.5.5 0 0 1 5 12.5V12h-.5a.5.5 0 0 1-.5-.5V11h-.5a.5.5 0 0 1-.468-.325z"/> </svg> </div> <div class="col-sm-3"> <!-- 复选框绑定当前任务的完成状态 --> <input type = "checkbox" [checked]="task.isCompleted" (click)="isChecked(task,i,$event)"> </div> </div> </td> </tr> </tbody> </table>
内容的提问来源于stack exchange,提问作者Mohit Kumar Sharma
相关产品推荐
相关产品推荐

