0

我正在尝试从我的子组件到父组件的 ag 网格表中发出过滤的行数

<template>
  <ag-grid-vue
    style="width: 100%; height: 600px"
    class="ag-theme-balham"
    id="myGrid"
    :enableRangeSelection="true"
    :defaultColDef="{
              resizable: true,
              sortable: true,
              filter: true,
              width: 100
            }"
    :columnDefs="columnDefs"
    :gridOptions="gridOptions"
    :processCellForClipboard="processCellForClipboard"
    :rowData="newRowData"
    :modules="[...agModule, ...agCModule]"
  ></ag-grid-vue>
</template>

<script>
import { AgGridVue } from "ag-grid-vue";
import "ag-grid-enterprise";
import { LicenseManager } from "ag-grid-enterprise";
import { AllModules } from "ag-grid-enterprise/dist/ag-grid-enterprise";
import { AllCommunityModules } from "ag-grid-community/dist/ag-grid-community";

LicenseManager.setLicenseKey(process.env.VUE_APP_AG_KEY);
import axios from "axios";

export default {
  name: "Table",
  props: {
    columnDefs: {
      type: Array,
      default() {
        return null;
      }
    },
    rowData: {
      type: Array,
      default() {
        return null;
      }
    }
  },
  components: {
    "ag-grid-vue": AgGridVue
  },
  data() {
    return {
      agModule: AllModules,
      agCModule: AllCommunityModules,
      newRowData: [],
      gridApi: null,
      gridOptions: {}
    };
  },
  watch: {
    rowData: function(newVal, oldVal) {
      this.newRowData = newVal;
    },
    count: "getDisplayedRowCount"
  },
  computed: {
    count() {
      return this.gridApi.getDisplayedRowCount();
    }
  },
  beforeMount() {
    this.processCellForClipboard = params => {
      return `${params.value.trim()},`;
    };
  },
  methods: {
    getDisplayedRowCount() {
      console.log("getDisplayedRowCount() => " + this.count);
      this.$emit("filteredrows", this.count);
    }
  },
  mounted() {
    this.newRowData = this.rowData;
    this.gridApi = this.gridOptions.api;
  }
};
</script>

<style lang="sass" scoped>
@import "../../../node_modules/ag-grid-community/dist/styles/ag-grid.css"
@import "../../../node_modules/ag-grid-community/dist/styles/ag-theme-balham.css"
</style>

这就是我的子组件的外观。但是当 ag 网格表加载时,该gridapi值为 null,因此我没有得到count在计算属性中定义的值。getDisplayedRowCount每次行数发生变化时,我都想调用该函数。我怎样才能做到这一点?

4

1 回答 1

1

你可以这样写你的计算属性

computed: {
    count() {
      if (this.gridApi) {
        return this.gridApi.getDisplayedRowCount();
      } else {
        return this.newRowData.length;
      }
    }
  },
于 2020-04-13T15:09:39.233 回答