基于element ui封装table组件

效果图:

1.封装表格代码如下

<template>
<div>
  <div class="TableList">
    <el-table
        v-loading="loading"
        @selection-change="selectionChange"
        class="table"
        :data="tableData"
        :border="hasBorder"
        :row-class-name="tableRowClassName"
        ref="multipleTable"
        :header-cell-style="{
				background: 'rgb(245 247 250)',
				color: 'rgb(51, 51, 51)',
			}"
    >
      <el-table-column type="selection" width="50" v-if="selectionShow"> </el-table-column>
      <!-- 序号 -->
      <el-table-column v-if="hasIndex" type="index" label="序号" header-align="center" align="center" width="80"> </el-table-column>
      <!-- 其他 -->
      <template v-for="item in tableColumnOptions">
        <!-- 插槽列 -->
        <el-table-column
            v-if="item.columnType"
            :key="item.label"
            :prop="item.prop"
            :label="item.label"
            :width="item.width"
            :header-align="item.headerAlign || 'center'"
            :align="item.align || 'center'"
        >
          <template slot-scope="{ row }">
            <slot :name="item.soltName" :data="row"></slot>
          </template>
        </el-table-column>
        <!-- 非插槽列 -->
        <el-table-column
            v-else
            :key="item.label"
            :prop="item.prop"
            :label="item.label"
            :width="item.width"
            :header-align="item.headerAlign || 'center'"
            :align="item.align || 'center'"
        >
          <!--          表头的特殊处理(eg:表头上加提示或图标等)-->
          <template slot="header" v-if="item.showIcon">
            <div class="tab-header">
              {
  { item.label }}
              <el-tooltip :content="item.content" placement="top">
                <i  class="el-icon-warning-outline icon"></i>
              </el-tooltip>
            </div>
          </template>
        </el-table-column>
      </template>
      <el-table-column type="selection" width="50" v-if="selectionShow1" :selectable="checkboxT"> </el-table-column>
    </el-table>
  </div>
</div>
</template>
<script>
export default {
  props:{
    // 表格数据
    tableData: {
      type: Array,
      default() {
        return [];
      },
    },
    // 表格列项
    tableColumnOptions: {
      type: Array,
      require: true,
      default() {
        return [];
      },
    },
    // 是否具有索引
    hasIndex: {
      type: Boolean,
      default() {
        return true;
      },
    },
    // 是否显示边框
    hasBorder: {
      type: Boolean,
      default() {
        return false;
      },
    },
    // 是否显示选择框
    selectionShow: {
      type: Boolean,
      default() {
        return true;
      },
    },
    //加载
    loading: {
      type: Boolean,
      default: false,
    },
  },
  methods:{
    // 选择框改变
    selectionChange(e){
      this.$emit('selectionChange',e)
    },
    tableRowClassName({ row, rowIndex }) {},
  }
}
</script>
<style scoped lang="scss">

</style>

2.在页面上使用

<template>
<div>
  <tableList
      :selection-show="true"
      :loading="loading"
      :has-index="false"
      :has-border="true"
      :table-data="tableData"
      :table-column-options="tableColumnOptions"
      @selectionChange="selectionChange"
  >
    <!-- 类型插槽 -->
    <template #tag="{ data }">
      <div class="tag">
        <el-tag type="success" v-if="data.type === 1">tag1</el-tag>
        <el-tag type="danger" v-if="data.type === 0">tag2</el-tag>
      </div>
    </template>
    <!-- 操作插槽 -->
    <template #operation="{ data }">
      <el-button type="text" @click="handleEdit(data)">编辑</el-button>
      <el-button type="text" @click="handleDelete(data)" id="delete">删除</el-button>
    </template>
  </tableList>
</div>
</template>
<script>
import tableList from '@/views/daily-examine/components/table/index.vue';
export default {
  components:{
    tableList
  },
  data(){
    return {
      loading:false,
      tableData:[
        { id: 202391074073, name: "张三", type: 1,special:'1'},
        { id: 202391074071, name: "李四", type: 0,special:'1'},
        { id: 202391074074, name: "王二", type: 0,special:'1'},
        { id: 202391074074, name: "马六", type: 0,special:'1'},
        { id: 202391074074, name: "李六", type: 1,special:'1'},
      ],
      tableColumnOptions:[
        { label: "账号", prop: "id" },
        { label: "姓名", prop: "name" },
        { label: "tag", prop: "type", columnType: true, soltName: "tag" },
        { label: "表头特殊处理",prop:'special',showIcon:true, content:'提示,提示,提示', },
        { label: "操作", width: "300", columnType: true, soltName: "operation"},
      ],

    }
  },
  methods:{
    selectionChange(){

    },
    //编辑
    handleEdit(data){},
    //删除
    handleDelete(data){},

  }
}
</script>
<style scoped lang="scss">

</style>

以上就是基于element ui封装table组件

相关推荐

  1. elemeentui el-table封装

    2023-12-29 12:48:01       46 阅读
  2. elementUI Table点击取当前行索引

    2023-12-29 12:48:01       43 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-29 12:48:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-29 12:48:01       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-29 12:48:01       82 阅读
  4. Python语言-面向对象

    2023-12-29 12:48:01       91 阅读

热门阅读

  1. TypeScript 基础知识:基本数据类型

    2023-12-29 12:48:01       57 阅读
  2. UNIAPP中借助store+watch完成实时数据

    2023-12-29 12:48:01       45 阅读
  3. uni-app绘制二维码

    2023-12-29 12:48:01       55 阅读
  4. 初始JVM

    初始JVM

    2023-12-29 12:48:01      56 阅读
  5. ajax请求的详细流程+详细示例

    2023-12-29 12:48:01       57 阅读
  6. 算法:加油站

    2023-12-29 12:48:01       51 阅读
  7. 算法设计与分析 | N皇后问题

    2023-12-29 12:48:01       57 阅读
  8. 2023年的技术回顾

    2023-12-29 12:48:01       52 阅读
  9. MVCC(多版本并发控制)

    2023-12-29 12:48:01       42 阅读
  10. How to assert expected exceptions in pytest

    2023-12-29 12:48:01       59 阅读