Angular UI-Grid expandableRow selectionChange更新父级

时间:2015-04-14 09:06:39

标签: javascript angularjs angular-ui-grid

我使用Angular-UI Grid和ui-grid-expandable选项。 如果选择了可扩展(子)网格的一行,是否还可以选择父网格行? 如果不是至少我需要访问子(可扩展)网格的rowSelectionChanged事件中的父网格/行,它似乎没有自己的范围?

 gridApi.selection.on.rowSelectionChanged($scope, function (rows) {
             var selectedRows=gridApi.selection.getSelectedRows();
             ...
             //select parent grid row here...
              });

1 个答案:

答案 0 :(得分:3)

是的,有办法做到这一点。您只需向后移动范围以访问父行和父行的网格api。请记住,appScope始终引用网格的父作用域。因此,在可扩展子网格中,父作用域包含行对象。

在您的子网格的onRegisterApi处理程序中,您可以通过row.grid.appScope.row访问父行。然后,您可以选择父行:row.grid.appScope.row.grid.api.selection.selectRow(row.grid.appScope.row.entity)

这是一个清理它的代码示例:

onRegisterApi: function (api) {
  api.selection.on.rowSelectionChanged($scope, function (row, event) {
    var parentRow = row.grid.appScope.row;
    if (api.selection.getSelectedRows().length > 0) {
      parentRow.grid.api.selection.selectRow(parentRow.entity);
    }
    else {
      parentRow.grid.api.selection.unSelectRow(parentRow.entity);
    }
  });
}

一个演示插件:http://plnkr.co/edit/7HFHPTtmcXvgoJCKiRTd?p=preview