ag-grid - how to get parent node from child grid via context menu?

此生再无相见时 提交于 2019-12-13 20:57:17

问题


As an example, I have a Master\Detail grid.

Master\Detail defined as key-relation model and on getDetailRowData method parent node data exists in params

but how to get parent node data from child view?

Tried via context-menu:

On right click - getContextMenuItems got executed which has an input params

On this sample, child-grid doesn't have any row and node in context-params is null, it's ok, but anyway it should be possible to retrieve the parent details at least via grid API, isn't it ?

Then I've tried to get parent via node: but instead of detail_173 as you can see its ROOT_NODE_ID, and here is a confusion for me.

So question is that how to get parent node data (RecordID 173 just in case) through child-view context menu or any other possible way (without storing temp value, cuz multiple children's could be opened at the same time)

Yes, I've read this part Accessing Detail Grid API, and still unclear how to get parent-node via child-grid.


回答1:


A very reliable solution is to create your own detailCellRenderer.

on the init method:

this.masterNode = params.node.parent; 

when creating the detail grid:

detailGridOpions = {
    ...
    onCellClicked: params => console.log(this.masterNode.data)
}

Here is a plunker demonstrating this: https://next.plnkr.co/edit/8EIHpxQnlxsqe7EO




回答2:


Able to achieve it. Have a look at the plunk I've created: Get master record from child record - Editing Cells with Master / Detail

Right click on any child grid's cell, and check the console log. You'll be able to see parent record in the log for the child record on which you've click.


The implementation is somewhat tricky. We need to traverse the DOM inside our component code. Luckily ag-grid has provided us the access of it.

  1. Get the child grid's wrapper HTML node from the params - Here in the code, I get it as the 6th element of the gridOptionsWrapper.layoutElements array.

  2. Get it's 3rd level parent element - which is the actual row of the parent. Get it's row-id.

  3. Use it to get the row of the parent grid using parent grid's gridApi.

  getContextMenuItems: (params): void => {
      var masterId = params.node.gridOptionsWrapper.layoutElements[6]
            .parentElement.parentElement.parentElement.getAttribute('row-id');
      // get the parent's id
      var id = masterId.replace( /^\D+/g, '');
      console.log(id);
      var masterRecord = this.gridApi.getRowNode(id).data;
      console.log(masterRecord);
    },
    defaultColDef: { editable: true },
    onFirstDataRendered(params) {
      params.api.sizeColumnsToFit();
  }

Note: Master grid's rowIds are defined with [getRowNodeId]="getRowNodeId" assuming that account is the primary key of the parent grid.



来源:https://stackoverflow.com/questions/53722447/ag-grid-how-to-get-parent-node-from-child-grid-via-context-menu

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!