Search code examples
javascriptvue.jsag-gridag-grid-vueonrowclick

ag-grid-vue: how to detect row click?


I am following the example here.

All works and now I want to detect a row click and get the event's data.

Here is my App.vue:

<template>
  <ag-grid-vue style="width: 500px; height: 500px;"
           class="ag-theme-alpine"
           :columnDefs="columnDefs"
           :rowData="rowData"
           :rowClicked="onRowClicked"
  >
  </ag-grid-vue>
</template>

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

export default {
  name: 'App',
  data() {
    return {
      columnDefs: null,
      rowData: null
    }
  },
  components: {
    AgGridVue
  },
  methods:{
    onRowClicked(params) {
      console.log(params);
      console.log(params.node.data);
    }
  },
  beforeMount() {
    this.columnDefs = [
      {headerName: 'Make', field: 'make', sortable: true, filter: true },
      {headerName: 'Model', field: 'model', sortable: true, filter: true },
      {headerName: 'Price', field: 'price', sortable: true, filter: true }
    ];

    fetch('https://raw.githubusercontent.com/ag-grid/ag-grid/master/grid-packages/ag-grid-docs/src/sample-data/smallRowData.json')
        .then(result => result.json())
        .then(rowData => this.rowData = rowData);
  }
}
</script>

<style>
@import "../node_modules/ag-grid-community/dist/styles/ag-grid.css";
@import "../node_modules/ag-grid-community/dist/styles/ag-theme-alpine.css";
</style>

However, the method onRowClicked is not called when I click on a row.

What I am missing here?


Solution

  • Replace this line

    :rowClicked="onRowClicked"
    

    with

    @rowClicked="onRowClicked"
    

    To make the ag-grid-vue component emit the event when a row is selected.

    Live Demo

    Edit 64387203/ag-grid-vue-how-to-detect-row-click