加载中...
ag-grid-vue
发表于:2023-08-16 | 更新于:2023-08-23 | 分类: 组件

这是一个基于Vue.js的数据表格组件。
官网:https://www.ag-grid.com/vue-data-grid/vue2/

依赖下载

依赖下载
npm install --save ag-grid-community ag-grid-vue vue-property-decorator@^8.0.0
npm install --save ag-grid-enterprise
main.js
import 'ag-grid-enterprise';
简单使用
<template>
  <ag-grid-vue
    style="width: 500px; height: 200px"
    class="ag-theme-alpine"
    :columnDefs="columnDefs"
    :rowData="rowData"
    :autoGroupColumnDef="autoGroupColumnDef"
     @grid-ready="onGridReady"
  >
  </ag-grid-vue>
</template>

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

export default {
  name: "App",
  data() {
    return {
      columnDefs: null,
      rowData: null,
       autoGroupColumnDef: {
         headerName: 'Model',
         field: 'model',
         cellRenderer: 'agGroupCellRenderer',
         cellRendererParams: {
            checkbox: true
          }
        }
    };
  },
  components: {
    AgGridVue,
  },
  methods: {
    onGridReady(params) {
       this.gridApi = params.api;
       this.columnApi = params.columnApi;
      },
    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 = [
       { field: 'make', sortable: true, filter: true, checkboxSelection: true },
       { field: 'model', sortable: true, filter: true },
       { field: 'price', sortable: true, filter: true }
    ];

    this.rowData = [
      { make: "Toyota", model: "Celica", price: 35000 },
      { make: "Ford", model: "Mondeo", price: 32000 },
      { make: "Porsche", model: "Boxster", price: 72000 },
    ];
  },
};
</script>
<style lang="scss">
  @import "~ag-grid-community/styles/ag-grid.css";
  @import "~ag-grid-community/styles/ag-theme-alpine.css";
</style>

样式修改

表格样式

方法一

style直接改
<style lang="scss">
 @import "~ag-grid-community/styles/ag-grid.css"; 
 @import "~ag-grid-community/styles/ag-theme-alpine.css";

 // 表头背景
 .ag-header {
   background-color: #F7F9FE !important;
 }

 // 偶数表体背景
 .ag-row-odd{
   background-color: #eff7fe !important;
 }

 // 合计固定页脚样式问题
 .ag-pinned-left-floating-bottom{
   .ag-row-even{
     .indexColumn{
       color: transparent;
     }
   }
 }
 .ag-floating-bottom{
   .actions-cell{
     display: none !important;
   }
 }

 // 表体border
 .ag-ltr .ag-cell{
   border-right: 1px solid #bdc3c7 !important;
 }
 .ag-pinned-right-header .ag-header-cell-resize::after{
   left: 0%;
 }
 [col-id="indexColumn"]{
   .ag-header-cell-resize::after{
     height: 0px !important;
   }
 }

 // 表体点击border颜色
 .ag-ltr .ag-cell-focus:not(.ag-cell-range-selected):focus-within{
   border-color: #2196f3 !important;
 }

 // 表头右border
 .ag-header-cell-resize::after{
   top:8px !important;
   width: 1px !important;
   height: 50% !important;
 }
</style>

方法二

node_modules
[class*=ag-theme-] {
  --ag-cell-horizontal-border     表体border
}
.ag-theme-alpine, .ag-theme-alpine-dark {
  --ag-header-background-color             表头背景颜色
  --ag-header-column-resize-handle-height  表头border高度
  --ag-header-column-resize-handle-width   表头border宽度
  --ag-odd-row-background-color            奇数表体背景颜色
  --ag-background-color                    偶数表体背景颜色
}

配置项

配置选项 默认值 描述
headerName 自定义 表头名称
field 自定义 字段名称
gridOptions 对象 表格配置属性
defaultColDef 对象 表格默认列定义
cellRenderer 自定义组件 挂载组件
valueGetter node.rowIndex + 1 排列序号
pinned left,right 列固定
pinnedTopRowData 对象 顶部行固定
pinnedBottomRowData 对象 底部行固定
headerHeight 10 表头行高
rowHeight 10 表体行高
width 10 表体宽度
initialWidth 10 初始宽度
minWidth 10 最小宽度
maxWidth 10 最大宽度
sortable false 表格排序
filter false 表格过滤
suppressMenu false 表头分组
resizable false 列宽启动自适应
suppressSizeToFit false 阻止自适应宽度
suppressAutoSize false 阻止自动调整大小
rowDrag false 行拖放
checkboxSelection false 表体勾选
headerCheckboxSelection false 表头勾选
cellClass 类名自定义 表格class
下一篇:
ERP_BUG
本文目录
本文目录