ProTable API

作者:陈佳鑫
时间:2023-07-06

cjx-zdy-ui项目仓库open in new window

图片展示


代码展示

注意

在VuePress2中使用存在样式问题,建议clone仓库下来运行查看

No Data
Total 0
  • 1
Go to
点击查看代码
<template>
  <ProTable
    ref="proTableRef"
    :columns="columns"
    :initParam="{ winRate: 100 }"
    :tabs="{ data: getTabsList }"
    rowKey="lotteryActivityAwardId"
    :children-prop="{
      lazy: true,
      load: getChildren
    }"
    :span-method="mergeColum"
    :request-api="getList"
    :beforeSearchSubmit="formatParams"
    :dataCallback="formatData"
  >
    <template #tableHeader="{ selectedListIds, selectedList, isSelected }">
      <el-button type="primary">导出报表</el-button>
      <el-button type="primary" :disabled="!isSelected">批量删除</el-button>
    </template>
    <template #action="{ row }">
      <el-button link type="primary"
        >编辑{{ row.lotteryActivityAwardId }}</el-button
      >
    </template>
  </ProTable>
</template>
<script lang="ts" setup>
import { computed, onMounted, ref } from 'vue'
import { ProTable } from 'cjx-zdy-ui'
import { ColumnProps } from 'cjx-zdy-ui/es/src/proTable/types'
import { TableColumnCtx } from 'element-plus/es/components/table/src/table-column/defaults'

const getTabsList = () => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        data: [
          { label: '测001', name: 1 },
          { label: '测002', name: 2 }
        ]
      })
    }, 5000)
  })
}

const formatParams = (params: any) => {
  console.log(params)
  return { ...params, 2: 1 }
}

const formatData = (data: any) => {
  console.log(data)
  return data
}

const getList = (params: any) => {
  console.log(params)
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve({
        pageNum: 1,
        pageSize: 10,
        total: 6,
        data: [
          {
            lotteryActivityAwardId: 1,
            awardName: { a: '测试1', b: '测试2' },
            winRate: 1000,
            totalNum: 100,
            num: 20,
            usedNum: 10,
            status: 1
            // hasChildren: true,
          },
          {
            lotteryActivityAwardId: 2,
            awardName: '奖品二',
            winRate: null,
            totalNum: 100,
            num: 20,
            usedNum: 20,
            status: 1
          },
          {
            lotteryActivityAwardId: 3,
            awardName: '奖品三',
            winRate: 1000,
            totalNum: 100,
            num: 20,
            usedNum: 30,
            status: 1
          },
          {
            lotteryActivityAwardId: 4,
            awardName: '奖品四',
            winRate: 1000,
            totalNum: 100,
            num: 20,
            usedNum: 40,
            status: 1
          },
          {
            lotteryActivityAwardId: 5,
            awardName: '奖品五',
            winRate: 1000,
            totalNum: 100,
            num: 20,
            usedNum: 50,
            status: 0
          },
          {
            lotteryActivityAwardId: 6,
            awardName: '奖品六',
            winRate: 1000,
            totalNum: 100,
            num: 20,
            usedNum: 60,
            status: 1
          }
        ]
      })
    }, 3000)
  })
}

const getChildren = (
  row: any,
  treeNode: unknown,
  resolve: (data: any[]) => void
) => {
  setTimeout(() => {
    resolve([
      {
        lotteryActivityAwardId: 11,
        awardName: { a: '测试3', b: '测试4    ' },
        winRate: 1000,
        totalNum: 100,
        num: 20,
        usedNum: 20
      }
    ])
  }, 5000)
}

const mergeColum = ({
  row,
  rowIndex,
  column,
  columnIndex
}: {
  row: any
  rowIndex: number
  column: TableColumnCtx<any>
  columnIndex: number
}) => {
  if (columnIndex === 1) {
    if (rowIndex % 2 === 0) {
      return {
        rowspan: 2,
        colspan: 1
      }
    } else {
      return {
        rowspan: 0,
        colspan: 0
      }
    }
  }
}

const proTableRef = ref(null)

const columns = computed((): ColumnProps[] => [
  { label: '', prop: 'lotteryActivityAwardId', type: 'selection' },
  { label: '序号', prop: 'lotteryActivityAwardId' },
  {
    label: '奖品名',
    prop: 'awardName',
    _children: [
      {
        label: '测试1',
        prop: 'awardName.a',
        search: { el: 'input' }
      },
      { label: '测试2', prop: 'awardName.b', search: { el: 'input' } }
    ]
  },
  {
    label: '概率(总和100%)',
    prop: 'winRate',
    formatter: (row: any) => {
      return row.winRate != null ? row.winRate / 100 + '%' : '未设置'
    }
  },
  {
    label: '总数量',
    prop: 'totalNum',
    search: { el: 'select', defaultValue: 1, props: { clearable: true } },
    enum: () => {
      return new Promise((resolve) => {
        setTimeout(() => {
          resolve({
            data: [
              { label: '测001', value: 1 },
              { label: '测002', value: 2 }
            ]
          })
        }, 5000)
      })
    },
    isFilterEnum: false
  },
  {
    label: '剩余数量',
    prop: 'num',
    search: {
      el: 'date-picker',
      props: {
        format: 'YYYY-MM-DD HH:mm:ss',
        type: 'datetimerange',
        defaultTime: [
          new Date('2020-01-1 00:00:00'),
          new Date('2020-01-1 23:59:59')
        ]
      }
    }
  },
  { label: '抽取数量', prop: 'usedNum', sortable: 'custom' },
  {
    label: '状态',
    prop: 'status',
    isTag: true,
    enum: [
      {
        userLabel: '启用',
        userStatus: 1,
        tagType: 'success',
        label: '',
        value: ''
      },
      {
        userLabel: '禁用',
        userStatus: 0,
        tagType: 'danger',
        label: '',
        value: ''
      }
    ],
    fieldNames: { label: 'userLabel', value: 'userStatus' }
  },
  { label: '操作', prop: 'action' }
])
</script>

安装

npm i cjx-zdy-ui

yarn install cjx-zdy-ui

引用 main.ts

import CjxUI from 'cjx-zdy-ui'

app.use(CjxUI)

ProTable 属性

属性名说明类型必传可选值默认值
columns列配置项ColumnProps[]trueColumnProps-
data静态 table data 数据,若存在则不会使用 requestApi 返回的 dataany[]false--
requestApi请求表格数据的 apiPromise<any>false--
requestAuto是否自动执行请求 apibooleanfalsebooleanfalse
requestError表格 api 请求错误监听(params: any) => voidfalse--
beforeSearchSubmitapi 请求参数格式化beforeSearchSubmit?: (params: any) => {}false--
dataCallback返回数据的回调函数,可以对数据进行处理(data: any) => anyfalse--
pagination是否需要分页组件booleanfalsebooleantrue
initParam初始化请求参数{}false-{}
border是否带有纵向边框booleanfalsebooleantrue
toolButton是否显示表格功能按钮booleanfalsebooleantrue
rowKey行数据的 Key,用来优化 Table 的渲染,当表格数据多选时,所指定的 idstringfalse-id
searchCol表格搜索项 每列占比配置number Record<BreakPoint, number>false-{ xs: 1, sm: 2, md: 2, lg: 3, xl: 4 }
tabs表格头部tabs切换配置TabsPropsfalse--
childrenProp树形数据配置TabsPropsfalse--
spanMethod合并行或列的计算方法({ row, column, rowIndex, columnIndex }) => number[] { rowspan: number colspan: number } undefinedfalse--

ColumnProps 属性

注意

ColumnProps 继承了TableColumnCtx 此处只写明额外属性,请结合element-plus Table API 文档一起看

属性名说明类型必传可选值默认值
isTag是否是标签展示booleanfalseboolean-
hideInTable是在表格当中隐藏booleanfalsebooleanfalse
search搜索项配置, 如果未传将不在搜索条件中显示SearchPropsfalse--
enum枚举类型(字典)EnumProps[] ((params?: any) => Promise<any>)false--
isFilterEnum当前单元格值是否根据 enum 格式化booleanfalse-true
fieldNames指定 label value children 的 key 值FieldNamesPropsfalse--
headerRender自定义表头内容渲染(tsx语法)(scope: HeaderRenderScope<T>) => VNodefalse--
render自定义单元格内容渲染(tsx语法)(scope: RenderScope<T>) => VNode stringfalse--
_children多级表头ColumnProps[]false--

TabsProps 属性

属性名说明类型必传可选值默认值
datatabs渲染数据Array<{ label: string; name: string number }> () => Promise<any>true--
tabsKey查询主键,如未传入该字段,字段为statusstringfalse-status
option字典类型keyName,未传入取值 label name{ labelKey: string; nameKey: string }false-{ labelKey: label; nameKey: name }

childrenProp 属性

属性名说明类型必传可选值默认值
lazy是否懒加载子节点数据booleanfalse-false
load加载子节点数据的函数,lazy 为 true 时生效(row: any, treeNode: unknown, resolve: (data: any[]) => void) => voidfalse--
treeProps渲染嵌套数据的配置选项{ children: string, hasChildren: string }false-{ hasChildren: 'hasChildren', children: 'children' }

SearchProps 属性

属性名说明类型必传可选值默认值
el当前项搜索框的类型input input-number select select-v2 tree-select cascader date-picker time-picker time-select switch slider--
props搜索项参数,根据 element plus 官方文档来传递,该属性所有值会透传到组件anyfalse--
key当搜索项 key 不为 prop 属性时,可通过 key 指定stringfalse--
order搜索项排序(从大到小)numberfalse--
span搜索项所占用的列数,默认为1列numberfalse-1
offset搜索字段左侧偏移列数numberfalse--
defaultValue搜索项默认值string number boolean any[]false--

EnumProps 属性

属性名说明类型必传可选值默认值
label选项框显示的文字stringtrue--
value选项框值stringtrue--
disabled是否禁用此选项booleanfalse--
tagType当 tag 为 true 时,此选择会指定 tag 显示类型success info warning dangerfalse--
children为树形选择时,可以通过 children 属性指定子选项EnumProps[]false--

FieldNamesProps 属性

属性名说明类型必传可选值默认值
label选项框显示的文字stringtrue--
value选项框值stringtrue--
children选项框子选项stringfalse--
Last Updated:
Contributors: 1102166242@qq.com