zorro表格tanle可编辑行,可添加行,删除避坑

9 篇文章 0 订阅
1 篇文章 0 订阅

可直接赋值官网上面的可编辑行的代码后进行修改

 <nz-table
        class="table-content mB12"
        #editRowTable
        nzBordered
        [nzData]="doctorTable"
        nzTableLayout="fixed"
        [nzFrontPagination]="false"
        [nzShowPagination]="false"
        [nzScroll]="{ x: '100%', y: 'calc(100% - 330px)' }"
        nzTableLayout="fixed"
      >
        <thead>
          <tr>
            <th nzWidth="80px">工号</th>
            <th nzWidth="100px">姓名</th>
            <th nzWidth="120px">科室</th>
            <th nzWidth="120px">职称</th>
            <th nzWidth="130px">联系方式</th>
            <th nzWidth="70px">操作</th>
          </tr>
        </thead>
        <tbody>
          <tr *ngFor="let data of editRowTable.data">
            <ng-container *ngIf="!editCache[data.id].edit; else editTemplate">
              <td>{{ data.name }}</td>
              <td>{{ data.age }}</td>
              <td>{{ data.age }}</td>
              <td>{{ data.age }}</td>
              <td>{{ data.address }}</td>
              <td>
                <button nz-button nzType="text" nzDanger (click)="showConfirm()">
                  <i nz-icon nzType="delete-thin" nzTheme="outline"></i>
                </button>

                <a (click)="startEdit(data.id)">Edit</a>
              </td>
            </ng-container>
            <ng-template #editTemplate>
              <td><input type="text" nz-input [(ngModel)]="editCache[data.id].data.name" /></td>
              <td><input type="text" nz-input [(ngModel)]="editCache[data.id].data.age" /></td>
              <td><input type="text" nz-input [(ngModel)]="editCache[data.id].data.age" /></td>
              <td><input type="text" nz-input [(ngModel)]="editCache[data.id].data.age" /></td>
              <td><input type="text" nz-input [(ngModel)]="editCache[data.id].data.address" /></td>
              <td>
                <button nz-button nzType="primaryText" (click)="saveEdit(data.id)">保存</button>
             

                <a nz-popconfirm nzPopconfirmTitle="Sure to cancel?" (nzOnConfirm)="cancelEdit(data.id)">取消</a>
              </td>
            </ng-template>
          </tr>
        </tbody>
      </nz-table>

      <button nz-button nzType="biz" style="margin-left: 12px" (click)="addRow()">
        <i nz-icon nzType="add" nzTheme="outline"></i>
      </button>
  // 医生表格
  doctorTable: any[] = [
    { id: '1', name: '11', age: 11, address: '11' },
    { id: '2', name: '22', age: 22, address: '22' }
  ];

  editCache: { [key: string]: { edit: boolean; data: ItemData } } = {};

  startEdit(id: string): void {
    this.editCache[id].edit = true;
  }

  // 新增
  addRow(): void {
    // 可取表格最后一条id+1
    this.doctorTable = [
      ...this.doctorTable,
      {
        id: '21',
        name: ``,
        age: 0,
        address: ``
      }
    ];

    this.editCache['21'] = {
      data: {
        id: '21',
        name: ` `,
        age: 0,
        address: ``
      },
      edit: true
    };
    console.log(this.editCache);
  }

  // 取消编辑
  cancelEdit(id: string): void {
        this.doctorTable = this.doctorTable.filter((d) => d.id !== id);
    // this.doctorTable.splice(this.doctorTable.length - 1, 1);

    delete this.editCache[id];
  }

  saveEdit(id: string): void {
    const index = this.doctorTable.findIndex((item) => item.id === id);
    Object.assign(this.doctorTable[index], this.editCache[id].data);
    this.editCache[id].edit = false;
  }

  // 更新该项是否是编辑状态
  updateEditCache(): void {
    this.doctorTable.forEach((item) => {
      this.editCache[item.id] = {
        edit: false,
        data: { ...item }
      };
    });
  }

  // 删除
  showConfirm(): void {
    this.modal.confirm({
      nzTitle: '提示',
      nzContent: '确定删除么',
      nzOnOk: () => console.log('OK')
    });
  }
  ngOnInit(): void {
    this.updateEditCache();
  }

在删除的地方遇到了一个坑

本来我使用splice方法来删除表格数据的,this.doctorTable.splice(this.doctorTable.length - 1, 1),但是项目会报错

主要原因是splice方法把原数组修改掉了 所以报错了 使用filter函数不会改变原数组 ,后改用filter

cancelEdit

这个方法不够完善, 我现在运用到是没用编辑的,只需要新增删除,可以再加一个字段判断一下是新增的编辑还是编辑

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值