2013-09-28 35 views
4

我正在ng网格中定制一个单元格模板。在那个单元格中,我想要一个按钮来触发某个需要将行索引放入原始数据数组的事件。模板看起来是这样的:ng-grid原始行索引

<button class="btn" ng-click="removeItem(row.rowIndex)"> 
    <i class="icon-remove"></i> 
</button> 

removeItem是这样实现的:

$scope.removeItem = function(rowIndex) { $scope.myList.splice(rowIndex, 1) } 

这工作,直到我通过点击列的一个重新排序网格。显然,rowIndex是行的可视化索引,而不是我提供的数组中行的索引。

有没有办法获得实际的索引?

回答

5

我能想到的一个简单方法是在模型数据本身上添加属性索引,并在获取数据时对其进行初始化。这样你总是有最初的行顺序。像

angular.forEach(items,function(item,index){ 
    item.index=index; 
}); 

我不认为网格提供任何这样的机制。

4

灵感来自ngGrid - remove row

你可以找到使用indexOf(row.entity)

HTML

<input type="button" value="remove" ng-click="removeRow(row)" /> 

的Javascript

$scope.removeRow = function(row) { 
    var index = $scope.myData.indexOf(row.entity); 
    $scope.myData.splice(index, 1); 
}; 

Full example on Plunker

元素的原始索引
+0

这很有道理,尽管我更喜欢一个不及时的线性解决方案。 – thesamet