vxe-table简单使用 vue vxe-table vue整合vxe-table vue2 vxe-table 简单使用

vxe-table简单使用 vue vxe-table vue整合vxe-table vue2 vxe-table 简单使用

安装vxe-table

vue2稳定版本
vue3稳定版本

npm install xe-utils vxe-table@legacy

安装 vxe-table 依赖的插件

npm i xe-utils

引用

我这边是全局引入,可以根据自己情况按需引入
main.js


      import Vue from 'vue'
      import VXETable from 'vxe-table'
      import 'vxe-table/lib/style.css'

      Vue.use(VXETable)

      // 给 vue 实例挂载内部对象,例如:
      // Vue.prototype.$XModal = VXETable.modal
      // Vue.prototype.$XPrint = VXETable.print
      // Vue.prototype.$XSaveFile = VXETable.saveFile
      // Vue.prototype.$XReadFile = VXETable.readFile

使用

我这边使用的 demo 用到了 虚拟滚动,列筛选自定义列内容 ,可直接全复制

<!-- 页面 -->
<template>
 		<vxe-button @click="clearFilter">清除所有的筛选条件</vxe-button>
       <!--    表格-->
      <div class="main-table-div">
        <vxe-table
          ref="xTable1"
          size="mini"
          border
          max-height="800"
          :row-config="{ isHover: true, height: 60 }"
          :column-config="{ resizable: true }"
          :sort-config="{ trigger: 'cell' }"
          :data="tableData"
          align="center"
          show-overflow
          :scroll-x="{gt: 30}"
          :scroll-y="{gt: 300}"
          :filter-config="{remote: true}"
          @filter-change="filterChangeEvent"
        >
			
          <vxe-column title="机种名称" field="machineTypeName" width="200" :filters="[{ data: '' }]" :filter-method="filterMethod">
            <template #filter="{ $panel, column }">
            <!-- 官网给的示例是遍历  filters 属性值数组 ,但是一般搜索只有一个输入框,不需要遍历,尽量减少一点性能消耗,我这边使用固定数组第一个元素-->
            <!--  <input type="type" v-for="(option, index) in column.filters" :key="index" v-model="option.data" @input="$panel.changeOption($event, !!option.data, option)"> -->
              <input v-model="column.filters[0].data" type="type" @input="$panel.changeOption($event, !!column.filters[0].data, column.filters[0])">
            </template>
          </vxe-column>
          <vxe-column title="辅料名称" field="auxiliaryMaterialName" width="200" :filters="[{ data: '' }]" :filter-method="filterMethod">
            <template #filter="{ $panel, column }">
              <input v-model="column.filters[0].data" type="type" @input="$panel.changeOption($event, !!column.filters[0].data, column.filters[0])">
            </template>
          </vxe-column>
          <vxe-column title="品牌" field="brand" width="120" :filters="[{ data: '' }]" :filter-method="filterMethod">
            <template #filter="{ $panel, column }">
              <input v-model="column.filters[0].data" type="type" @input="$panel.changeOption($event, !!column.filters[0].data, column.filters[0])">
            </template>
          </vxe-column>
          <vxe-column title="规格型号" field="specification" width="150" :filters="[{ data: '' }]" :filter-method="filterMethod">
            <template #filter="{ $panel, column }">
              <input v-model="column.filters[0].data" type="type" @input="$panel.changeOption($event, !!column.filters[0].data, column.filters[0])">
            </template>
          </vxe-column>
          <vxe-column title="封装用量(ml/g)" field="packageAmount" width="130" :filters="[{ data: '' }]" :filter-method="filterMethod">
            <template #filter="{ $panel, column }">
              <input v-model="column.filters[0].data" type="type" @input="$panel.changeOption($event, !!column.filters[0].data, column.filters[0])">
            </template>
          </vxe-column>
          <vxe-column title="料号" field="materialNumber" width="150" :filters="[{ data: '' }]" :filter-method="filterMethod">
            <template #filter="{ $panel, column }">
              <input v-model="column.filters[0].data" type="type" @input="$panel.changeOption($event, !!column.filters[0].data, column.filters[0])">
            </template>
          </vxe-column>
          <vxe-column title="用量(ml/g)" field="dosage" width="120" :filters="[{ data: '' }]" :filter-method="filterMethod">
            <template #filter="{ $panel, column }">
              <input v-model="column.filters[0].data" type="type" @input="$panel.changeOption($event, !!column.filters[0].data, column.filters[0])">
            </template>
          </vxe-column>
          <vxe-column title="使用位置" field="usePosition" width="120" :filters="[{ data: '' }]" :filter-method="filterMethod">
            <template #filter="{ $panel, column }">
              <input v-model="column.filters[0].data" type="type" @input="$panel.changeOption($event, !!column.filters[0].data, column.filters[0])">
            </template>
          </vxe-column>
          <vxe-column title="其他说明" field="doc" width="120" show-header-overflow show-overflow="title" show-footer-overflow />
          <vxe-column title="创建时间" field="addTime" width="180" />
          <vxe-column title="创建人" field="addUserName" width="180" :filters="[{ data: '' }]" :filter-method="filterMethod">
            <template #filter="{ $panel, column }">
              <input v-model="column.filters[0].data" type="type" @input="$panel.changeOption($event, !!column.filters[0].data, column.filters[0])">
            </template>
          </vxe-column>
          <vxe-column title="最后修改时间" field="updateTime" width="180" />
          <vxe-column title="最后修改人" field="updateUserName" width="180" :filters="[{ data: '' }]" :filter-method="filterMethod">
            <template #filter="{ $panel, column }">
              <input v-model="column.filters[0].data" type="type" @input="$panel.changeOption($event, !!column.filters[0].data, column.filters[0])">
            </template>
          </vxe-column>
          <vxe-column title="操作" width="200" fixed="right">
            <template #default="{ row, rowIndex }">
              <div>
                <el-button size="small" type="text" @click="showChange(row)">修改</el-button>
                <el-button size="small" type="text" @click="deleteById(row, rowIndex)">删除</el-button>
              </div>
            </template>
          </vxe-column>
        </vxe-table>
      </div>
</template>

<script>
export default {
  name: 'Index',

  components: {},
  props: [],
  data() {
    return {
    
      tableData: [ {
                "id": 1006,
                "machineTypeName": "Cheng Fat",
                "auxiliaryMaterialName": "Cheng Fat",
                "brand": "aeaVNGZ7An",
                "specification": "IhABS5QBlX",
                "packageAmount": "14LBJIvbgZ",
                "materialNumber": "Ik8mzrfSWg",
                "dosage": "4FOZcY3KEF",
                "usePosition": "DWI5RHkBIY",
                "doc": "nT65qhLkvI",
                "addTime": "2017-09-10 00:22:20",
                "addUserName": "Cheng Fat",
                "updateTime": "2021-04-16 15:04:40",
                "updateUserName": "2005-09-24"
            },
            {
                "id": 1005,
                "machineTypeName": "Yuen Tsz Hin",
                "auxiliaryMaterialName": "Yuen Tsz Hin",
                "brand": "e7fPgpqElq",
                "specification": "l57klAlUd4",
                "packageAmount": "6Y4e7xcfWg",
                "materialNumber": "2FOmQSMC8H",
                "dosage": "TfdbMdn10B",
                "usePosition": "vFVoDlRbYI",
                "doc": "6sArmCG4oP",
                "addTime": "2002-08-12 05:47:08",
                "addUserName": "Yuen Tsz Hin",
                "updateTime": "2009-09-21 22:40:57",
                "updateUserName": "2001-04-10"
            }],
      search: {
        machineTypeName: '',
        auxiliaryMaterialName: '',
        brand: '',
        specification: '',
        packageAmount: '',
        materialNumber: '',
        dosage: '',
        usePosition: '',
        addUserName: '',
        updateUserName: ''
      }
    }
  },

  computed: {},

  watch: {},

  beforeMount() { },

  mounted() { },
  created() {
  },
  activated() {
  },
  methods: {
    /**
     *  当筛选条件发生变化时会触发该事件,api: https://vxetable.cn/v3/#/table/api?filterName=filter-change
     * @param {*} column 当前筛选的列属性
     * @param {*} property 当前筛选的列名称
     * @param {*} values 不知道,忽略
     * @param {*} datas 当前表格所有列的筛选数据值列表
     * @param {*} filterList 当前表格所有列的筛选数据列表,带列属性和值
     * @param {*} $event 事件
     */
    filterChangeEvent({ column, property, values, datas, filterList, $event }) {
      console.info('列筛选')
      // console.info('column', column)
      // console.info('property', property)
      // console.info('values', values)
      // console.info('datas', datas)
      console.info('filterList', filterList)
      // console.info('$event', $event)
      
      // 先清空之前的搜索对象信息
      this.search = {
        machineTypeName: '',
        auxiliaryMaterialName: '',
        brand: '',
        specification: '',
        packageAmount: '',
        materialNumber: '',
        dosage: '',
        usePosition: '',
        addUserName: '',
        updateUserName: ''
      }
      // 遍历当前表所有的筛选数据,赋值给搜索对象
      filterList.forEach(item => {
        // 将当前筛选的列条件数据赋值给 查询对象
        console.info(item)
        // item.property 当前筛选条件的列名称
        // item.datas[0] 当前筛选条件的列值 因为 每列的 filters 数组数据 只有一个元素,固定0
        this.search[item.property] = item.datas[0]
      })
      console.info('查询参数', this.search)
      // 将参数传递到后台进行查询

    },
    // 表格单个列筛选,这个筛选的是 当前表数据,不使用这个
    filterMethod({ value, row, column }) {
    },
    
    // 清空表格所有查询条件
    clearFilter() {
      this.$refs.xTable1.clearFilter()
      // 清空查询对象
      this.search = {
        machineTypeName: '',
        auxiliaryMaterialName: '',
        brand: '',
        specification: '',
        packageAmount: '',
        materialNumber: '',
        dosage: '',
        usePosition: '',
        addUserName: '',
        updateUserName: ''
      }
      // 重新请求服务端查询
      
    }
    
    // 删除
    deleteById(row, index) {
      console.info(row, index)
    },

    // 打开修改 弹出框
    showChange(row) {
      console.info(row)
    }
  }
}
</script>
<style lang="scss" scoped>
</style>

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
vxe-table是一个基于vue的表格组件,支持增删改查、虚拟滚动、懒加载、快捷菜单、数据校验、树形结构、打印导出、表单渲染、数据分页、模态窗口、自定义模板、灵活的配置项、丰富的扩展插件等... 设计理念: 面向现代浏览器,高效的简洁 API 设计 模块化表格、按需加载、插件化扩展 为单行编辑表格而设计,支持增删改查及更多扩展,强大的功能的同时兼具性能 功能: Basic table (基础表格) Grid (高级表格) Size (尺寸) Striped (斑马线条纹) Table with border (带边框) Cell style (单元格样式) Column resizable (列宽拖动) Maximum table height (最大高度) Resize height and width (响应式宽高) Fixed column (固定列) Grouping table head (表头分组) Highlight row and column (高亮行、列) Table sequence (序号) Radio (单选) Checkbox (多选) Sorting (排序) Filter (筛选) Rowspan and colspan (合并行或列) Footer summary (表尾合计) Import (导入) Export (导出) Print (打印) Show/Hide column (显示/隐藏列) Loading (加载中) Formatted content (格式化内容) Custom template (自定义模板) Context menu(快捷菜单) Virtual Scroller(虚拟滚动) Expandable row (展开行) Pager(分页) Form(表单) Toolbar(工具栏) Tree table (树形表格) Editable CRUD(增删改查) Validate(数据校验) Data Proxy(数据代理) Keyboard navigation(键盘导航) Modal window(模态窗口) Charts(图表工具) 更新日志: v4.0.20 table 修改单选框、复选框获取值错误问题 grid 修复 KeepAlive 中报错问题
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值