2016-06-10 40 views
1

我是Angular的新手,一直在试图制作一个动态表。Angular.js:表中的列的动态长度

用户输入他想要的列数,然后他可以根据需要添加多少行到每列列。

我在每列的末尾提供了一个“添加”按钮,用于向该特定列添加新行。

我的代码看起来有点像这样:

<table> 
 

 
    <tr> 
 
    <th ng-repeat="x in setno"> SET {{x.number}}</th> 
 
    </tr> 
 
    
 
    <tr ng-repeat="z in selected"> 
 
    <td> </td> 
 
    </tr> 
 
    
 
    <tr> 
 
    <td ng-repeat="x in setno"> 
 
     <button ng-click="selected.push({})"> add </button> 
 
    </td> 
 
    </tr> 
 
    
 
</table>

其中,setno是包含数字的列表:

$scope.setno[{id:"a", number:"1"},{id:"b", number:"2"},...]; 

选择具有相似的结构。

问题是,当我点击任何列的“”添加“按钮时,新行将被添加到所有列中。

虽然我想要添加一个新行,但只添加了“添加”按钮的那一行。

我怎么知道谁的“”按钮已被点击?

回答

0

我怎么知道谁的“添加”按钮已被点击?

您需要访问目标中继当前$index。现在,我不知道它是你努力的目标,但你可以在ng-repeater$index另一个变量存储使用以下语法内中继接入:

<!-- outer loop --> 
<div ng-repeat="(idxA, item) in items)"> 

    <!-- inner loop --> 
    <div ng-repeat"(idxB, obj) in item.objs"> 

     <ul> 
      <!-- another inner loop --> 
      <li ng-repeat="li in obj.pts"> 

在上面的代码idxA会与外环的$index相同,idxB是内环的$index等等。然后,您只需将索引(例如idxAidxB)传递给您的方法。

资源: