将element-ui table封装,和pagination封装到一起

news/2024/7/10 1:33:41 标签: vue

用过antdv的table组件之后,再用element 的table实在不方便,就封装了一下,模仿antdv的使用方法,将分页组件也封入到组件中

原始用法:

缺点:有多少列就要写多少个el-table-column, 分页组件也需要每次引入,同一段代码不断的重复写

  <template>
    <el-table
      :data="tableData"
      style="width: 100%">
      <el-table-column
        prop="date"
        label="日期"
        width="180">
      </el-table-column>
      <el-table-column
        prop="name"
        label="姓名"
        width="180">
      </el-table-column>
      <el-table-column
        prop="address"
        label="地址">
      </el-table-column>
    </el-table>
  </template>

封装之后的组件使用: 用法同antdv

<base-table
    :data="tableData"
    :stripe="true"
    :pagination="pagination"
    :table-title="tableTitle">
    <template #warnLevel="{row}">
      <span :style="{color: colorDict[row.warnLevel].color}">{{ colorDict[row.warnLevel].label }}</span>
    </template>
  </base-table>

tableTitle就是我们的所有列,该对象的所有属性同element ui 中的Table-column属性,例如:

const tableTitle = [
  {
    key: 'name',
    title: '设施名称',
    sortable: true
  },
  {
    key: 'comp',
    title: '设施类型',
    sortable: true
  },
  {
    key: 'county',
    title: '分公司',
    sortable: true
  },
  {
    key: 'town',
    title: '灾害类型',
    sortable: true
  },
  {
    key: 'town',
    title: '影响时间',
    sortable: true
  },
  {
    key: 'town',
    title: '灾害电压',
    sortable: true
  },
  {
    key: 'warnLevel',
    title: '预警等级',
    sortable: true,
    scopedSlots: { customRender: 'warnLevel' } // 个性化需要设置此字段
  }
];

下面提供一下完整的封装组件: 

<template>
  <div class="base-table-wrap">
    <el-table
      v-bind="$attrs"
      :class="[extraClass]"
      style="width: 100%"
      header-cell-class-name="jt-table-header"
      v-on="$listeners">
      <el-table-column
        v-for="(column, idx) in tableTitle"
        :key="idx"
        :label="column.title"
        :prop="column.key"
        :show-overflow-tooltip="tooltip"
        v-bind="column"
      >
        <template slot-scope="scope">
          <slot
            v-if="column.scopedSlots && column.scopedSlots.customRender"
            :name="column.scopedSlots ? column.scopedSlots.customRender : ''"
            v-bind="scope"
            v-on="$listeners"
          />
          <span v-else>{{ scope.row[column.key] }}</span>
        </template>


      </el-table-column>
    </el-table>
    <el-pagination
      v-if="pagination"
      :current-page="pagination.page"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pagination.limit"
      :total="pagination.total"
      background
      class="system-pagination"
      layout="prev, pager, next,sizes,jumper"
      @size-change="sizeChange"
      @current-change="pageChange"/>
  </div>


</template>

<script>
export default {
  name: 'BaseTable',
  props: {
    tableTitle: {
      type: Array,
      default: () => []
    },
    pagination: {
      type: [Object, Boolean],
      default: () => {}
    },
    sizeChange: {
      type: Function,
      default: () => {}
    },
    pageChange: {
      type: Function,
      default: () => {}
    },
    theme: {
      type: String,
      default: 'light'
    },
    tooltip: {
      type: Boolean,
      default: true
    },
    extraClass: {
      type: String,
      default: ''
    }
  }
};
</script>

<style  lang="scss">
.base-table-wrap::after {
  display: block;
  content: 'clear';
  clear: both;
  line-height: 0;
  visibility: hidden;
}
.jt-table-header {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.light {
  .el-table__header-wrapper {
    background-color: #f6f6f6;
  }
}
</style>

 

在main.js 中引入

import BaseTable from '@/components/Base/BaseTable';

Vue.component('base-table', BaseTable);

然后就可以在组件中直接<base-table/>使用了

 

注意: 使用过程中如果有什么问题可以发留言哈,如果有什么好的建议,也欢迎随便提哦


http://www.niftyadmin.cn/n/951524.html

相关文章

vue原理之-神奇的Object.defineProperty

这个方法了不起啊。。vue.js和avalon.js 都是通过它实现双向绑定的。。而且Object.observe也被草案发起人撤回了。。所以defineProperty更有必要了解一下了几行代码看他怎么用 var a {}Object.defineProperty(a,"b",{value:123})console.log(a.b);//123 很简单&…

vue3.0 ------ createApp()

vue3.0 ------ createApp() 在2.X版本中创建一个vue 实例是通过 new Vue()来实现的&#xff0c;到了3.X中则是通过使用createApp这个 API返回一个应用实例&#xff0c;并且可以通过链条的方式继续调用其他的方法 参数 components options root props 例如&#xff1a;我在main…

修改webkit内核浏览器滚动条样式(修改element-ui table样式)

webkit浏览器css设置滚动条主要有下面7个属性 1. ::-webkit-scrollbar 滚动条整体部分&#xff0c;可以设置宽度啥的 2. ::-webkit-scrollbar-button 滚动条两端的按钮 3. ::-webkit-scrollbar-track 外层轨道 4. ::-webkit-scrollbar-track-piece 内层滚动槽 5. ::-webkit-s…

vue3 reactive函数用法

reactive的用法与ref的用法相似&#xff0c;也是将数据变成响应式数据&#xff0c;当数据发生变化时UI也会自动更新。不同的是ref用于基本数据类型&#xff0c;而reactive是用于复杂数据类型&#xff0c;比如对象和数组 例如&#xff1a;定义一个对象类型的变量user <templ…

Vue Function-based API RFC

转载尤大大的文章&#xff1a;https://zhuanlan.zhihu.com/p/68477600 2020 年一月又注&#xff1a;RFC 已经被完全重写&#xff0c;最新版本请以 https://composition-api.vuejs.org/ 为准。以下内容会有部分与最新的 API 有出入&#xff0c;但依然可以帮助理解。 --- 译注…

[import ... from」、「 import ... = require()」 和 「import(path: string)」有什么区别?

1、import ... from2、import(path: string)3、import ... require() 前两个都是ES6的模块语法&#xff0c;第3个import ... require() 是ts的语法 使用export命令定义了模块的对外接口以后&#xff0c;其他 JS 文件就可以通过import命令加载这个模块 第1个&#xff1a;im…

TS之类型断言

类型断言用于手动指定一个值的类型。 一、语法 值 as 类型 二、用途 2.1 将一个联合类型断言为其中一个类型 interface Cat {name:string;run():void; } interface Fish {name:string;swim():void; } function getName(animal:Cat|Fish):string{return animal.name; } 只…

vue props和attrs

vue3 props 要先声明才能取值&#xff0c;attrs不用声明直接使用&#xff08;$attrs 包含 class and style attribute&#xff09;props 不包含事件&#xff0c;attrs包含 props 支持 String 以外的类型&#xff0c;attrs只有 String 类型 props 没有声明的属性&#xff0c;会…