How to get the data of selected row in ag grid in angular2?

后端 未结 6 1909
温柔的废话
温柔的废话 2021-02-14 05:09

I have setup ag-grid in angular2 which works fine but i am not able to get the value of selected row...There are no errors in my console window...This is how i am initialising t

6条回答
  •  离开以前
    2021-02-14 05:35

    Firstly, row selection must be enabled by setting gridOptions.rowSelection to either "single" or "mulitple", depending on the selection behavior you'd like to implement.

    You can then use the grid API method getSelectedNodes() to return a list of all currently selected rows in ag-Grid. Extracting the data from each node is as simple as mapping over each node and returning its data property.

    Here is the code when using a JavaScript framework:

    getSelectedRowData() {
        let selectedNodes = this.gridApi.getSelectedNodes();
        let selectedData = selectedNodes.map(node => node.data);
        alert(`Selected Nodes:\n${JSON.stringify(selectedData)}`);
        return selectedData;
    }
    

    You can also see this illustrated in the Angular/React/Vue.js examples below:

    Angular

    React

    Vue.js

    Vanilla JS
    Note: When using Vanilla JS, the gridApi and columnApi can be reached from the gridOptions Object.

    Read the full post on our blog or check out our documentation for a great variety of scenarios you can implement with ag-Grid.

    Ahmed Gadir | Developer @ ag-Grid

提交回复
热议问题