vue2使用ag-grid表格

ag-grid官网:Vue Grid: Custom Components | AG Grid

根据官方文档说的AG Grid no longer supports Vue 2. The last version to support Vue 2 is AG Grid v31.3.,目前只有v31.3.版本支持vue2。

以下是官方给的demo

Vue Grid: Get Started with AG Grid | AG GridIn this article, we will walk you through the necessary steps to add AG Grid (both are covered) to an existing Vue project, configure some of the essential features of it. We will show you some of the fundamentals of the grid (passing properties, using the API, etc). As a bonus, we will also tweak the grid's visual appearance using Sass variables. Download AG Grid v32.1.0 today: The best Vue Table & Vue Data Grid in the world.icon-default.png?t=O83Ahttps://www.ag-grid.com/vue-data-grid/vue2/#reference-App.vue

1、在vue组件中引入使用

<template>
  <ag-grid-vue
    style="width: 100%; height: 500px;"
    class="ag-theme-alpine"
    :columnDefs="columnDefs"
    :rowData="rowData"
    :gridOptions="gridOptions"
    @grid-ready="onGridReady"
    @cellEditingStopped="onCellEditingStopped"
    @cellClicked="handleCellClick"
  />
</template>

<script>
  import "ag-grid-community/styles/ag-grid.css";
  import "ag-grid-community/styles/ag-theme-balham.css";
  import { AgGridVue } from "ag-grid-vue/lib/main.cjs";
  export default {
    components: {
      AgGridVue
    },
    data() {
      return {
        columnDefs: [ // 表格列配置
          { // 勾选列
            headerName: "",
            checkboxSelection: true,
            headerCheckboxSelection: true,
            pinned: 'left',
            width: 50
          },
          {
            headerName: '序号',
            field: 'index',
            pinned: 'left', // 列固定到左边
            sortable: false, // 禁用自带排序,默认为true
            width: 60,
            suppressMovable: true, // 禁用拖拽表头,Set to true if you do not want this column to be movable via dragging.
            cellRenderer: function (params) { // 自定义单元格渲染内容
              return parseInt(params.node.id) + 1
            },
            cellStyle: {
              'text-align': 'center' // 表格文字居中, 不包括表头文字
            }
          },
          {
            headerName: '回复备注',
            suppressMovable: true,
            field: "confirmComment",
            editable: (params) => { // 是否可编辑,true/false/function
              // console.log(params)
              return params.data.action === 'Confirm(回复)' && this.userType === 'VENDOR'
            },
            sortable: false,
            width: 150,
            cellStyle: {
              'text-align': 'center'
            }
          },
          {
            headerName: '数量',
            suppressMovable: true,
            field: "inProcessQty",
            editable: (params) => {
              // console.log(params)
              return params.data.action === 'Confirm(回复)' && this.userType === 'VENDOR'
            },
            cellDataType: 'number', // 数据类型,
            sortable: false,
            width: 150,
            cellStyle: {
              'text-align': 'center'
            }
          },
          {
            headerName: '第一周',
            suppressMovable: true,
            sortable: false,
            cellStyle: {
              'text-align': 'center'
            },
            children: [
              {
                headerName: '第一天',
                suppressMovable: true,
                sortable: false,
                cellStyle: {
                  'text-align': 'center'
                },
                children: [
                  {
                    headerName: '9.2白班',
                    suppressMovable: true,
                    field: "day",
                    sortable: false,
                    cellStyle: {
                      'text-align': 'center'
                    },
                  },
                  {
                    headerName: '9.2夜班',
                    suppressMovable: true,
                    field: "night",
                    sortable: false,
                    cellStyle: {
                      'text-align': 'center'
                    },
                  }
                ]
              }
            ]
          }
        ],
        rowData: [], // 表格数据
        gridOptions: { // 表格相关配置
          enableCellTextSelection: true, // 允许复制单元格内容
          enableRangeSelection: true,
          rowSelection: 'multiple',  // 可勾选多行
          suppressRowClickSelection: true,
        }
      }
    },
    created() {
      this.getTableData()
    },
    methods: {
      onGridReady(params) {
        this.gridApi = params.api
        console.log(params, 'params')
      },
      getTableData(){
        this.$http({url: 'xxx'}).then(res => {
          this.gridApi.setColumnDefs(res);
        })
      },
      onCellEditingStopped(event){
        console.log(event, 'cell stop edit')
      },
      handleCellClick(event){
        console.log(event, 'click cell')
      },
    }
  };
</script>

<style scoped lang="scss">
  .the_returnedGoodsNoticeList_wrapper {
    & /deep/ .el-form .el-row .el-form-item:first-child {
      width:100%
    }
  }
  .ag-theme-balham {
    /* disable all borders */
    /*--ag-borders: none;*/
    /* then add back a border between rows */
    --ag-row-border-style: solid;
    --ag-row-border-width: 1px;
    --ag-row-border-color: #dfe6ec;
    /* and between columns */
    --ag-cell-horizontal-border: #dfe6ec;
  }
  .ag-theme-balham {
    --ag-header-height: 30px;
    --ag-header-foreground-color: black;
    --ag-header-background-color:  #88c1f4;
    --ag-cell-horizontal-border: solid #dfe6ec;
    /*--ag-header-cell-hover-background-color: rgb(80, 40, 140);*/
    /*--ag-header-cell-moving-background-color: rgb(80, 40, 140);*/
    --ag-header-column-separator-display: block;
    --ag-header-column-separator-height: 100%;
    /*--ag-header-column-separator-width: 2px;*/
    --ag-header-column-separator-color: white;

    --ag-header-column-resize-handle-display: block;
    --ag-header-column-resize-handle-height: 25%;
    /*--ag-header-column-resize-handle-width: 5px;*/
    --ag-header-column-resize-handle-color: white;
  }
  /* 固定列右边框 */
  /deep/ .ag-pinned-left-header {
    border-right: 1px solid white;
  }
 /* .ag-theme-balham .ag-header {
    font-family: cursive;
  }
  .ag-theme-balham .ag-header-group-cell {
    font-weight: normal;
    font-size: 22px;
  }
  .ag-theme-balham .ag-header-cell {
    font-size: 18px;
  }*/
  /*表头文字居中*/
  /deep/ .ag-theme-balham [class^='ag-'],
  .ag-theme-balham [class^='ag-']:focus,
  .ag-theme-balham [class^='ag-']:after,
  .ag-theme-balham [class^='ag-']:before {
    text-align: center;
  }
  /deep/ .ag-header-cell-text{
    text-align: center ;
    width: 100%;
  }
  /deep/ .ag-header-cell{
    border-top: 1px solid white !important;
  }
  /deep/ .ag-header-group-cell-with-group {
    border-top: 1px solid white !important;
  }
  /deep/ .ag-header-group-cell {
    justify-content: center;
  }
</style>

以上是我实践的demo,其中需要注意的点有:

1、包的引入,我与官方给的不一样,因为我按照官方方法报错了,

2、需要勾选列,要在columnDefs里面配置这一列

3、代码中的css包括修改表头背景、表头文字居中、表格边框

效果差不多如下图:

我这只是皮毛,基础用法,ag-grid还有很多很多功能属性有待探索。

ag-grid-vue3 是一个用于 Vue3 的数据表格组件,而 element-plus 是一个基于 Vue3 的 UI 组件库。虽然这两个组件库可以独立使用,但是它们可以一起使用以增强 UI 功能。 首先,你需要在项目中安装 element-plus,可以使用 npm 或 yarn 安装,如下所示: ``` npm install element-plus --save ``` 或 ``` yarn add element-plus ``` 安装完成后,在 Vue3 中使用 element-plus 组件,你可以在 main.js 文件中使用以下代码: ```javascript import { createApp } from 'vue' import App from './App.vue' import ElementPlus from 'element-plus' import 'element-plus/lib/theme-chalk/index.css' const app = createApp(App) app.use(ElementPlus) app.mount('#app') ``` 这里我们在 main.js 中导入了 ElementPlus 组件,并使用 app.use(ElementPlus) 将组件注册到 Vue3 中。注意,为了让样式生效,你需要在 main.js 中引入 element-plus/lib/theme-chalk/index.css。 接下来,在 ag-grid-vue3 中使用 element-plus 组件,你可以在 ag-grid-vue3 的 columnDefs 中使用 element-plus 的组件,例如: ```javascript { headerName: '姓名', field: 'name', cellRendererFramework: { template: '<el-button>{{ value }}</el-button>', data() { return { value: '' } }, mounted() { this.value = this.params.value } } } ``` 这里我们在 ag-grid-vue3 的 columnDefs 中使用了 element-plus 的 el-button 组件作为单元格渲染器,这样我们就可以在 ag-grid-vue3 中使用 element-plus 的组件了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值