基于element ui的表格的二次封装

本文详细介绍了基于ElementUI的表格组件进行二次封装的方法,包括如何配置显示数据、边框、序号、下拉展开、分页及加载动画等功能,同时提供了列属性、插槽和事件的使用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

日常练习篇      基于element ui的表格的简单二次封装

 

Table Attributes

 

参数说明类型默认值
tableData显示的数据array 
showBorder是否带有纵向边框booleanfalse

columns

列属性object 

indexOptions

序号

object

 

expandOptions

是否展示下拉

boolean

false
showPagination是否展示分页booleanfalse
loadingOptions是否有加载动画object 

currentPage

当前页number 1

pageSizes

每页显示个数选择器的选项设置number[]

[10, 20, 30, 40]

pageSize

每页显示条目个数number

10

total

总条目数number 
​
Table配置项

 <xwTable
      :tableData="rolesList"
      :columns="columns"
      :index-options="indexOptions"
      :showBorder="showBorder"
      :expand-options="true"
      :loadingOptions="loadingOptions"
      :showPagination="false"
    ></xwTable>

​

Table-column Attributes

参数说明类型
prop对应列内容的字段名string
label显示的标题string

align

对齐方式left / center / right

slot

string

(prop 和solt 不能共用)

​

    //列配置项     注意一般数据和插槽的区别
     columns: [
        {
          prop: "roleName",            //一般数据    
          label: "职位",
          align: "center"
        },
        {
          prop: "roleDesc",            //一般数据
          label: "描述",
          align: "center"
        },
        {
          label: "操作",                //插槽
          slot: "operation",
          align: "center"
        }
      ],

​

​插槽的使用: 注意v-slot绑定的值
<template v-slot:operation="scope">
    <el-button type="primary" @click="edit(scope.scope.row)" size="mini">编辑</el-           
    button>
    <el-button type="danger" @click="del(scope.scope.row)" size="mini">删除</el- 
     button>
     <el-button type="warning" @click="allocate(scope.scope.row)" size="mini">分配角 
     色</el-button>
</template>

loadingOptions Attributes

参数说明类型默认值

text

显示在加载图标下方的加载文案string

icon

显示加载图标图标

有默认

bgColor

遮罩背景色string有默认
  //加载配置项
     loadingOptions: {
        text: "加载中...",
        icon: "el-icon-loading",
        bgColor: "rgba(0, 0, 0, 0.8)"
      },

 

indexOptions Attributes

参数说明类型默认值

showIndex

是否显示序号booleantrue

label

显示的标题string 

align

对齐方式 stringleft / center /right

IndexMethod

方法  
      //序号配置项
      indexOptions: {
        showIndex: true,
        label: "#",
        align: "center",
        IndexMethod(index) {
          return index + 1;
        }
      },

 

Table Events

 

事件名称说明回调参数

handleSizeChange

pageSize 改变时会触发每页条数

handleCurrentChange

currentPage 改变时会触发当前页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
    }

 

总代码

<template>
  <div>
    <el-table
      :data="tableData"
      :border="showBorder"
      :element-loading-text="loadingOptions.text"
      :element-loading-spinner="loadingOptions.icon"
      :element-loading-background="loadingOptions.bgColor"
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange"
      v-loading="!tableData.length"
    >
      <!-- 是否展示下拉 -->
      <el-table-column v-if="expandOptions" type="expand">
        <template slot-scope="scope">
          <slot name="expand" :scope="scope"></slot>
        </template>
      </el-table-column>
      <!-- 是否展示序号 -->
      <el-table-column
        v-if="indexOptions&& indexOptions.showIndex"
        type="index"
        :label="indexOptions.label"
        :align="indexOptions.align"
        :index="indexOptions.indexMethod"
      ></el-table-column>
      <!-- 一般数据正常循环 -->
      <template v-for="(item,index) in columns">
        <el-table-column
          v-if="item.prop"
          :prop="item.prop"
          :label="item.label"
          :key="index"
          :align="item.align"
        >
        </el-table-column>
        <!-- 具名插槽放置按钮 -->
        <el-table-column  v-else :key="index" :label="item.label" :align="item.align">
          <template slot-scope="scope">
            <slot :name="item.slot" :scope="scope"></slot>
          </template>
        </el-table-column>
      </template>
    </el-table>
    <!-- 分页 -->
    <div v-if="showPagination" class="pagination">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="pageSizes"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
export default {
  name: "",
  props: {
    tableData: {
      //表格数据
      type: Array,
      requrequired: true
    },
    columns: {
      //字段名
      type: Array,
      requrequired: true
    },
    showBorder: {
      //是否带有边框
      type: Boolean,
      default: false
    },
    total: {
      //分页总数据
      type: [String, Number],
      default: 0
    },
    indexOptions: {
      //序号
      type: Object
    },
    expandOptions: {
      //是否展示下拉
      type: Boolean
    },
    showPagination: {
      //是否展示分页
      type: Boolean,
      default: true
    },
    currentPage: {
      //当前页
      type: Number,
      default: 1
    },
    pageSizes: {
      //当前页数数量
      type: Array,
      default: () => [10, 20, 30, 40]
    },
    pageSize: {
      //当前页数大小
      type: Number,
      default: 10
    },
    loadingOptions: {
      //加载
      type: Object,
      default: () => {
        return {
          text: "",
          icon: "el-icon-loading",
          bgColor: "hsla(0,0%,100%,.9)"
        };
      }
    },
  },
  data() {
    return {};
  },
  components: {},
  methods: {
    //改变
    handleSizeChange(val) {
      this.$emit("handleSizeChange", val);
    },
    handleCurrentChange(val) {
      this.$emit("handleCurrentChange", val);
    },
  },
  mounted() {},
  watch: {},
  computed: {}
};
</script>

<style scoped lang='scss'>
</style>

 

显示效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值