user3083872
user3083872

Reputation: 79

Cannot access ag-Grid API in Vuejs

I'm doing the simple "Get Started with ag-Grid in Your Vue Project" on the ag-grid website and have run into a problem. At a certain step in the tutorial, it tries to use the gridReady prop on the ag-grid-vue to execute a function called onGridReady. But this seems to never fire.

I have followed the instructions exactly as stated here: https://www.ag-grid.com/vue-getting-started/?utm_source=ag-grid-readme&utm_medium=repository&utm_campaign=github

App.vue:

<template>
    <div>
        <button @click="getSelectedRows()">Get Selected Rows</button>

        <ag-grid-vue style="width: 500px; height: 500px;"
                     class="ag-theme-balham"
                     :columnDefs="columnDefs"
                     :rowData="rowData"
                     rowSelection="multiple"

                     :gridReady="onGridReady">
        </ag-grid-vue>
    </div>
</template>

<script>
    import {AgGridVue} from "ag-grid-vue";

    export default {
        name: 'App',
        data() {
            return {
                columnDefs: null,
                rowData: null
            }
        },
        components: {
            AgGridVue
        },
        watch: {
          onGridReady: {
            handler: function (params) {
              alert('eeeee')
            },
            deep: true
          }
        },
        methods: {
            onGridReady(params) {
                this.gridApi = params.api;
                this.columnApi = params.columnApi;
                alert('TEST')
            },
            getSelectedRows() {
                const selectedNodes = this.gridApi.getSelectedNodes();
                const selectedData = selectedNodes.map( node => node.data );
                const selectedDataStringPresentation = selectedData.map( node => node.make + ' ' + node.model).join(', ');
                alert(`Selected nodes: ${selectedDataStringPresentation}`);
            }
        },
        beforeMount() {
            this.columnDefs = [
                {headerName: 'Make', field: 'make', checkboxSelection: true},
                {headerName: 'Model', field: 'model'},
                {headerName: 'Price', field: 'price'}
            ];

            fetch('https://api.myjson.com/bins/15psn9')
                .then(result => result.json())
                .then(rowData => this.rowData = rowData);
        }
    }
</script>

<style lang="scss">
@import "~ag-grid-community/dist/styles/ag-grid.css";
@import "~ag-grid-community/dist/styles/ag-theme-balham.css";
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

I expect to be able to click the button and see and alert of the selected nodes. What is get is an error message: Uncaught TypeError: Cannot read property 'getSelectedNodes' of undefined at VueComponent.getSelectedRows (VM2832 App.vue:54)

Upvotes: 2

Views: 2322

Answers (1)

thirtydot
thirtydot

Reputation: 228162

Use this instead, to bind an event (in Vue):

@gridReady="onGridReady"

See:

Upvotes: 4

Related Questions