简体   繁体   中英

How to get display rows data from ag-grid in vue?

I'm using ag-grid in my vue application.

I have three items in my data:

在此处输入图片说明

After I filter by "Toyota" I get only one data on the grid.

在此处输入图片说明

I want when I click on the button I'll get the filter results (like in the grid) but when I invoke this function I get all the items. this.$refs.grid.gridOptions.rowData ;

how to get only the data I can see from the grid?

app.js:

import "ag-grid-community/dist/styles/ag-grid.css";
import "ag-grid-community/dist/styles/ag-theme-balham.css";
import { AgGridVue } from "ag-grid-vue";

export const App = {
  name: "App",
  data() {
    return {
      columnDefs: null,
      rowData: null
    };
  },
  components: {
    AgGridVue
  },
  methods: {
    some: function() {
      const grid = this.$refs.grid.gridOptions;

      const rows = grid.rowData;
      console.log({ rows });
    }
  },
  beforeMount() {
    this.columnDefs = [
      { headerName: "Make", field: "make", filter: true },
      { headerName: "Model", field: "model" },
      { headerName: "Price", field: "price" }
    ];

    this.rowData = [
      { make: "Toyota", model: "Celica", price: 35000 },
      { make: "Ford", model: "Mondeo", price: 32000 },
      { make: "Porsche", model: "Boxter", price: 72000 }
    ];
  },
  template: `
    <div>
    <ag-grid-vue ref="grid" style="width: 500px; height: 300px;"
        class="ag-theme-balham"
        :columnDefs="columnDefs"
        :rowData="rowData">
    </ag-grid-vue>

    <button @click="some">Click</button>
    </div>
    `
};

main.js:

import Vue from 'vue';
import { App } from './app';

new Vue({
  el: '#root',
  render: h => h(App)
});

I don't think ag-grid has a direct way to get that data. But you can use forEachNodeAfterFilter to loop over the data and put together your own array.

const rowData = [];
gridApi.forEachNodeAfterFilter(node => rowData.push(node.data));

See https://www.ag-grid.com/javascript-grid-api/#accessing-row-nodes

You can get this by using this code:

for (let i = 0; i < grid.api.getDisplayedRowCount(); i++) {
  let node = grid.api.getDisplayedRowAtIndex(i);
  console.log(node.data);
}

This way you can iterate through the displayed rows, and if you want you can then push this data into another array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM