【问题标题】:ng-grid original row indexng-grid 原始行索引
【发布时间】:2013-09-29 14:50:15
【问题描述】:

我正在 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 是行的视觉索引,而不是我提供的数组中行的索引。

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

【问题讨论】:

    标签: angularjs ng-grid


    【解决方案1】:

    我能想到的一种简单方法是在模型数据本身上添加一个属性索引,并在获取数据时对其进行初始化。这样,您始终拥有初始行顺序。类似的东西

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

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

    【讨论】:

      【解决方案2】:

      灵感来自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

      【讨论】:

      • 这是有道理的,尽管我更喜欢时间上不是线性的解决方案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-07
      • 1970-01-01
      • 2015-04-09
      • 1970-01-01
      • 2022-01-18
      • 1970-01-01
      • 2021-07-04
      相关资源
      最近更新 更多