Element table组件封装 核心:父子组件传值、传方法

简介: 本文介绍了如何基于Element UI的table组件进行二次封装,创建一个具有父子组件传值和传方法功能的自定义表格组件,并提供了封装后的组件如何引入、注册和使用的方法。

element

最近在APP上使用了element的表格,原因是vant上没table组件,所以在element的基础上简单的封装了一个组件

组件 tableElem.vue

<template >
  <el-table
    id="immunityTadle"
    size="mini"
    :data="tableData"
    border
    :max-height="clientHeight"
    style="width: 100vw"
    @row-click="handleClick"
  >
    <!-- fixed  ==> 左边定位传true,右边定位传right; Type: String  -->
    <!-- //sortable  ==> 传true 有排序按钮 Type: Number -->
    <!-- align    对齐方式        left/center/right    Type String -->

       <!-- 对数量进项排序 -->
      <!-- :sort-method="(a, b) => sortHandle(a, b,item.sortType)" -->
    <el-table-column
      v-for="(item, index) in propList"
      :key="index"
      :align="item.align  ? item.align : 'left'"
      :sortable="item.sortable ? true : false"
      :fixed="
        item.fixed == 'true' ? true : item.fixed == 'right' ? 'right' : false
      "
      :prop="item.prop"
      :width="item.width"
      :label="item.label"
    ></el-table-column>
    <!-- :width="item.width" -->
    <el-table-column label="操作" width="80" v-if="isShow">
      <template slot-scope="scope">
        <el-button @click="handleClick(scope.row)" type="text" size="small">查看</el-button>
        <!-- <el-button type="text" size="small">编辑</el-button> -->
      </template>
    </el-table-column>
  </el-table>
</template>
<script>
export default {
   
  //propList   列的信息
  //tableData   表格的信息
  //isShow  是否显示编辑
  //clientHeight  高度  固定列得话必传  否则出现问题
  // /align 默认左  type strinng  center right left
  /*
     <table-elem
     :clientHeight="clientHeight"
     :isShow="isShow"
     :propList="propList"
     :tableData="tableData"
     @clickRow="clickRow"
     ></table-elem>
    //使用计算属性计算

    //50:单元格的高  40:thead的高    242: 当前页面表格上方的高(变量)
    computed: {
    clientHeight() {
            return this.tableData.length * 50 + 40 + 242 > window.screen.height
        ? window.screen.height - 242
        : this.tableData.length * 50 + 40
    }
  }

  使用分页加载的方式  属性:clientHeight    方法 getPullPageBottom   必传
   getPullPageBottom 父级对的方法
  */
  props: ['propList', 'tableData', 'isShow', 'clientHeight'],
  data() {
   
    return {
   }
  },
  methods: {
   
    //对数量进行排序
    // sortHandle(a, b, sortType) {
   
    //   if (!sortType) return
    //   return a[sortType] - b[sortType]
    // },
    //点击当前行信息 传给父组件
    handleClick(row) {
   
      this.$emit('clickRow', row)
    },
    pullScrollFun() {
   
      if (!this.$listeners['getPullPageBottom']) return
      let tableTbodyDom = document
        .getElementById('immunityTadle')
        .getElementsByClassName('el-table__body-wrapper')[0]
      let that = this
      let timer = true
      tableTbodyDom.addEventListener('scroll', function() {
   
        if (
          tableTbodyDom.scrollHeight -
            tableTbodyDom.scrollTop -
            this.clientHeight <=
          2
        ) {
   
          if (timer == true) {
   
            that.$emit('getPullPageBottom')
            timer = false
            setTimeout(() => {
   
              timer = true
            }, 1500)
          }
        }
      })
    }
  },
  mounted() {
   
    this.pullScrollFun()
  }
}
</script>
<style lang="less">
#immunityTadle::-webkit-scrollbar {
   
  width: 0 !important;
}
#immunityTadle {
   
  z-index: 0;
  height: 100%;
  overflow: auto !important;
  border-top-left-radius: 12px;
  border-top-right-radius: 12px;
  thead tr {
   
  }
  thead tr th {
   
    padding: 0;
    font-size: 12px;
    color: #4b4f4d;
    border: none;
    border-bottom: 1px solid #e9e9e9;
    .cell {
   
      text-align: center;
      word-break: normal;
      height: 40px;
      line-height: 40px;
    }
  }
  tbody tr td {
   
    padding: 0;
    font-size: 12px;
    color: #222;
    border: none;
    .cell {
   
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      height: 50px;
      line-height: 50px;
    }
  }
}
.el-table {
   
  overflow-y: scroll;
}
.el-table::before {
   
  height: 0px !important;
}
</style>
<style lang="less" scoped>
// /deep/ thead tr {
   
//   padding: 2px 0 !important;
//   text-align: center;
// }
// /deep/ .el-table tr td {
   
//   padding: 2px !important;
// }
// /deep/.el-table thead tr th {
   
//   height: 40px !important;
//   min-width: 50px !important;
//   // padding: 2px 0 !important;
//   font-size: 14px;
//   text-align: center;
// }
// /deep/ tbody tr {
   
//   padding: 1px !important;
//   text-align: center;
//   td {
   
//     padding: 1px !important;
//     text-align: center;
//     .cell {
   
//       // height: 30px !important;
//       padding: 1px !important;
//       font-size: 14px;
//     }
//   }
// }
</style>

引入组件

import TableElem from '@C/immunityCom/tableElem'

注册

  components: {
   
    TableElem
  },

使用:

    <table-elem
        :clientHeight="clientHeight"
        :isShow="isShow"
        :propList="propList"
        :tableData="tableData"
        @clickRow="clickRow"
        @getPullPageBottom="getPullPageBottom"
      ></table-elem>
      propList: [
        {
   
          prop: 'animalType',
          label: '种类',
          width: '80',
          fixed: 'true',
          align: 'center',
        },
        {
    prop: 'immuneDate', label: '时间', width: '100', sortable: true },
        {
    prop: 'vaccineType', label: '种类', width: '130' },
        {
   
          prop: 'annimateOughtCount',
          label: '数量A',
          width: '100',
          // sortable: true,
          align: 'center',
          // sortType:"oughtCount"
        },
        {
   
          prop: 'annimateCount',
          label: '数量B',
          width: '100',
          // sortable: true,
          align: 'center',
          // sortType:"count"
        },
      ],
      tableData:[] //具体数据 字段名要和propList 的prop一致

在这里插入图片描述

目录
相关文章
|
存储 NoSQL 搜索推荐
若依框架----源码分析(@RateLimiter)
若依框架----源码分析(@RateLimiter)
1395 0
|
传感器 监控 物联网
golang开源的可嵌入应用程序高性能的MQTT服务
golang开源的可嵌入应用程序高性能的MQTT服务
1388 3
|
JavaScript
Vue中如何实现兄弟组件之间的通信
在Vue中,兄弟组件可通过父组件中转、事件总线、Vuex/Pinia或provide/inject实现通信。小型项目推荐父组件中转或事件总线,大型项目建议使用Pinia等状态管理工具,确保数据流清晰可控,避免内存泄漏。
1067 2
基于element-plus实现vue3+ts后台管理系统的组件封装(只需传入配置对象,就可以渲染出一个页面(表单+表格))
基于element-plus实现vue3+ts后台管理系统的组件封装(只需传入配置对象,就可以渲染出一个页面(表单+表格))
|
网络协议 Linux 测试技术
Kali Linux 加入 Windows 域:完整的域渗透测试环境搭建指南
本文详述将Kali Linux加入Windows域的完整过程,涵盖DNS、Kerberos、SSSD配置及域枚举、渗透测试技术,打造专业内网渗透环境,适用于红队演练与安全研究。(238字)
986 10
|
IDE Linux 开发工具
IntelliJ IDEA最新版安装下载教程及安装教程(附安装包)
本文介绍IDEA的下载与安装教程,包含获取下载地址、安装步骤及激活方法。需注意安装路径为英文目录,运行激活脚本时需管理员权限。按指引操作即可完成激活并使用。
23543 0
|
SQL 数据采集 自然语言处理
04_用LLM分析数据:从表格到可视化报告
在当今数据驱动的时代,数据分析和可视化已成为商业决策、科学研究和日常工作中不可或缺的部分。随着大型语言模型(LLM)技术的飞速发展,2025年的数据分析领域正经历一场革命。传统的数据处理流程通常需要数据科学家掌握复杂的编程技能和统计知识,而现在,借助先进的LLM技术,即使是非技术人员也能轻松地从原始数据中获取洞见并创建专业的可视化报告。
1118 1
|
前端开发 JavaScript 开发者
利用 el-select 和 el-tree 实现树形结构多选框联动功能
本文详细介绍了如何使用ElementUI中的el-select下拉选择器和el-tree树形控件来实现多功能联动选择器,包括多选、删除、搜索、清空选项等功能。通过树形控件展示复杂的层级结构,用户可以通过下拉选择树形节点,实时搜索节点,且支持批量选择和删除功能。文中提供了完整的HTML、JavaScript和CSS代码实现,帮助开发者快速集成此功能。
6867 0
利用 el-select 和 el-tree 实现树形结构多选框联动功能
|
JavaScript Java 应用服务中间件
Vue 跨域问题 的几种解决办法 (No ‘Access-Control-Allow-Origin‘ header is present on the requested resource)
Vue 跨域问题 的几种解决办法 (No ‘Access-Control-Allow-Origin‘ header is present on the requested resource)
3192 0
Vue 跨域问题 的几种解决办法 (No ‘Access-Control-Allow-Origin‘ header is present on the requested resource)