如何解决角度可编辑索引问题>>

问题描述 投票:0回答:1

我在div和同一个表中有两个ngFor,一个ngFor数组保存一些现有数据,而第二个ngFor数组保存我要编辑的新数据,然后将数据推送到旧的默认/现有数据的第一个数组。

我的问题是,当我单击第二个数组中的一行上的编辑按钮时,该数组的相同索引/行也会被吸引进行编辑。我希望每个数组的索引都独立,也就是说,当我单击任一数组的索引上的编辑按钮时,仅应激活该索引,我该如何解决?下面是我的代码:

html

<div>
 <table >
   <tr>//.....</tr>
         // olds existing data, in which the data in the "newData" array with later land
   <tr *ngFor="list of existingData"; let i=index;>
      <td><button type="button" class="btn btn-danger"
          (click)=remove(i) [disabled]="disabled">
           <i class="glyphicon glyphicon-remove"></i>
          </button></td>
      <td><span>{{list.type}}</span><input type="test" *ngIf="enableEdit && enableEditIndex == i"></td>
      <td><button type="button"
        *ngIf="i != enableEditIndex || enableEditIndex == null"
        (click)="enableEditMethod($event, i )">
        <i class="glyphicon glyphicon-pencil"></i>
      </button></td>
   </tr>
               // here is the second tr
   <tr *ngFor="list1 of newData">
      <td><button type="button" class="btn btn-danger"
          (click)=remove(i)>
           <i class="glyphicon glyphicon-remove"></i>
          </button></td>
      <td><span>{{list1.type}}</span><input type="test" *ngIf="enableEdit && enableEditIndex == i"></td>
      <td><button type="button"
        *ngIf="i != enableEditIndex || enableEditIndex == null"
        (click)="enableEditMethod($event, i )">
        <i class="glyphicon glyphicon-pencil"></i>
      </button></td>
   </tr>
 </table>
</div>

// this is array from which data is push to "newData" array
<div>
 <table >
   <tr>//.....</tr>
   <tr *ngFor="allData of ArrayAllData">
      <td><button type="button" class="btn btn-secondary"
          (click)=addObj(i)>
           <i class="glyphicon glyphicon-plus"></i>
          </button></td>
      <td>{{allData.type}}</td>
   </tr>
 </table>
</div>

为什么我有两个数组是“ existingData”具有取消按钮,而“ newData”没有。

ts

.....//some declarations

enableEditMethod( e, i ) {
   this.enableEdit = true; 
   this.enableEditIndex = i; 
}

我在两个按钮上都使用相同的功能。如有疑问,请询问!

我在div和同一个表中有两个ngFor,一个ngFor数组保存一些现有数据,第二个ngFor数组保存我要编辑的新数据,然后再将数据推送到较旧的第一个数组...

html arrays angular
1个回答
-1
投票

我很确定这是因为您尚未为第二个*ngFor初始化第二个索引变量。

© www.soinside.com 2019 - 2024. All rights reserved.