在 ag grid vue 中发出过滤后的行数
Emit filtered rows count in ag grid vue
我正在尝试从 ag 网格 table 向父组件发出过滤后的行数,这是我的子组件
<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 网格 table 加载时,gridapi
值为空,因此我没有得到计算 属性 中定义的 count
的值。每次行数发生变化时,我都想调用函数 getDisplayedRowCount
。我怎样才能做到这一点?
你可以这样写你的计算属性
computed: {
count() {
if (this.gridApi) {
return this.gridApi.getDisplayedRowCount();
} else {
return this.newRowData.length;
}
}
},
我正在尝试从 ag 网格 table 向父组件发出过滤后的行数,这是我的子组件
<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 网格 table 加载时,gridapi
值为空,因此我没有得到计算 属性 中定义的 count
的值。每次行数发生变化时,我都想调用函数 getDisplayedRowCount
。我怎样才能做到这一点?
你可以这样写你的计算属性
computed: {
count() {
if (this.gridApi) {
return this.gridApi.getDisplayedRowCount();
} else {
return this.newRowData.length;
}
}
},