【问题标题】:Preserve selection in angular ui-grid while updating data在更新数据时保留角度 ui-grid 中的选择
【发布时间】:2016-03-24 06:32:20
【问题描述】:

http://plnkr.co/edit/r9hMZk?p=preview

我有一个 ui-grid,我在其中启用了多选。我希望能够在保留选择的同时更新数据。如果我只是更新数据,则不会保留选择。

$scope.gridOpts.data = data2;

但是我已经定义了一个 rowIdentity 函数,以便 id 列唯一标识一行。

"rowIdentity" : function(row) {
  return row.id;
}

现在,如果我选择 id=Bob 和 id=Lorraine 的行,然后更新数据,这些行仍然被选中。但是,这些行中的其他字段不会更新。

我怎样才能既保留选择又更新所有数据?

【问题讨论】:

    标签: angularjs angular-ui-grid


    【解决方案1】:

    我认为您需要自己跟踪您的 ID。因此,您应该删除 rowIdentifier,并在您的 swapData 函数的开头添加这一段。

        $scope.selIds = [];
        for (var selRow of $scope.gridApi.selection.getSelectedRows()) {
          $scope.selIds.push(selRow.id);
        }
    

    除此之外,在rowsRendered 上添加一个事件处理程序以重新选择之前选择的行

      gridApi.core.on.rowsRendered($scope,function() {
        for (var selId of $scope.selIds) {
          for (var row of $scope.gridOpts.data) {
            if (selId == row.id) {
              $scope.gridApi.selection.selectRow(row);
            }
          }
        }
      });
    

    你可以把它放在你的 registerApi 回调中。

    【讨论】:

    • 有效!我需要添加 $scope.gridApi.selection.unSelectRow(selRow);以避免“选定项目”计数不断增加。我只是想知道是否有更好的解决方案。
    • 如果标识与先前的数据集匹配,则优化不更新网格行上的任何内容可能是一个设计决策。只有开发人员知道..
    猜你喜欢
    • 1970-01-01
    • 2015-05-28
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2017-11-08
    • 2023-03-24
    • 2016-02-01
    • 1970-01-01
    相关资源
    最近更新 更多