vue3 + antd二次封装a-table组件

前置条件

vue版本 v3.3.11
ant-design-vue版本 v4.1.1

内容梗概

二次封装a-table组件,大大提高工作效率和降低项目维护成本;

先看效果图

在这里插入图片描述

代码区域

utils.js文件

// 用于模拟接口请求
export const getRemoteTableData = (data = [], time = 1000) => {
   
   
    return new Promise((resolve) => {
   
   
        setTimeout(() => {
   
   
            const retObj = {
   
   
                list: data,
                total: 100,
                pageSize: 10,
                pageNum: 1,
            }
            resolve(retObj)
        }, time)
    })
}

// 指定范围随机数
export const getRandomNumber = (min, max) => {
   
   
    return Math.floor(Math.random() * (max - min + 1) + min);
}

// 判断空值
export const isEmpty = (value) => {
   
   
    if (Array.isArray(value)) {
   
   
        return !value.length
    } else if (Object.prototype.toString.call(value) === "[object Object]") {
   
   
        return !Object.keys(value).length
    } else {
   
   
        return [null, undefined, ''].includes(value)
    }
}

//  数字格式化
export const formatNumber = (num) =>
    num ? (num + "").replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, "$&,") : (isEmpty(num) ? '' : 0)

// 百分比格式化
export const formatPercent = (percent, n = 2) => isEmpty(percent) ? '' : `${
     
     (+percent).toFixed(n)}%`

// 金额格式化
export const formatMoney = (num) => isEmpty(num) ? "" : formatNumber(num.toFixed(2))

my-table.vue组件

<template>
  <div>
    <div class="attach-buttons-wrap">
      <a-button
        style="margin-right: 10px"
        v-bind="btn.props"
        @click="btn.onclick({ selectedRowKeys })"
        v-for="btn in attachButtons"
        :key="btn.name"
        :disabled="
          typeof btn?.props?.disabled === 'function'
            ? btn?.props?.disabled(selectedRowKeys)
            : btn?.props?.disabled ?? false
        "
        >{
  
  { btn.name }}</a-button
      >
    </div>
    <a-table
      v-bind="{
        loading: localLoading,
        pagination: localPagination,
        rowSelection: localRowSelection,
        rowKey: 'id',
        ...$attrs,
      }"
      :dataSource="dataSource"
    >
      <!-- 自定义渲染单元格 -->
      <template #bodyCell="{ text, record, index, column }">
        <!-- 操作列 -->
        <template v-if="column.dataIndex === 'operation'">
          <a
            v-for="(item, itemIndex) in column.buttons"
            :key="itemIndex
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值