将element-puls表格改为 adt写法

132 阅读5分钟

构思:相信用element的小伙伴都有一个烦恼,就是element-puls的表格标签太多了,很容易看错,所以我就魔改了一下。通过vue3的v-for循环el-table中的el-table-column标签,实现一些功能。

需要实现功能: 实现单选功能 element的表格单选选中看着很别扭,没有单选框,我们给加上。 当表单需要一对多的时候,使用表格可以有校验。 可以拖拉位置。

image.png

封装组件

插件安装 引入

安装插件 实现拖动 yarn add sortablejs 安装 uuid yarn add uuid

引入插件 import Sortable from 'sortablejs'

import { v4 as uuidv4 } from 'uuid'

预设可能用到的参数 通过defineProps传入

首先需要keyid 这是element的原生属性,这里我们做来唯一标识,当单选或者多选时 带出对应属性。 data表格数据。 column枚举出el-table-column的标签和一些功能。 其他属性太多不一一解释了。

const props = defineProps({
  // 列表主键id(必须设置且自动带出)
  keyId: {
    required: false,
    type: String,
    default: 'id',
  },
  // 数据
  data: {
    required: true,
    type: Array,
    default: () => [],
  },
  /**
   *  列表每一行
   */
  column: {
    required: true,
    type: Array,
    default: () => [],
  },
  // 是否展开
  defaultExpandAll: {
    type: Boolean,
    default: false,
  },
  // 拖动class(需要提供本身或上级calss)
  className: {
    type: String,
    default: '',
    required: false,
  },
  //   加载状态
  loading: {
    type: Boolean,
    default: false,
    required: false,
  },
  //   分页总量
  total: {
    type: String || Number,
    default: 0,
    required: false,
  },
  //   高度
  height: {
    type: String,
    default: '40rem',
    required: false,
  },
  //   值为空是 显示
  empty: {
    type: String,
    default: '--',
    required: false,
  },
  //   是否需要序号
  index: {
    type: Boolean,
    default: true,
    required: false,
  },
  //   是否需要复选
  isSel: {
    type: Boolean,
    default: true,
  },
  // 是否需要边框
  border: {
    type: Boolean,
    default: true,
  },
  // 是否需要合计
  summary: {
    type: Boolean,
    default: false,
  },
  // 合计key数组 传入后自动计算
  amountList: {
    type: Array,
    default: () => [],
  },
  // 如果需要自己调整合计 请传入summaryMethodYes true
  summaryMethodYes: {
    type: Boolean,
    default: false,
  },
  // 合计计算 (自调用 带出一个对象值)
  summaryMethod: {
    type: Function,
    default: () => [],
  },
  stripe: {
    type: Boolean,
    default: true,
  },
  // 是否需要树结构
  isTree: {
    type: Boolean,
    default: false,
  },
  // 树结构列表时 子集
  treeProp: {
    type: Object,
    default: {
      children: 'children',
      hasChildren: 'hasChildren',
    },
  },
  // 是否需要单选(单选必须传入keyId,用作判断)
  isRadio: {
    type: Boolean,
    default: false,
  },
  // 是否需要分页
  isPage: {
    type: Boolean,
    default: true,
  },
  // 数据总量
  total: {
    required: true,
    type: Number,
  },
  // 页数
  page: {
    type: Number,
    default: 1,
  },
  // 每页数量
  limit: {
    type: Number,
    default: 20,
  },
  // 默认给出选项每页页数
  pageSizes: {
    type: Array,
    default() {
      return [10, 20, 30, 40, 50, 100, 200, 300, 500]
    },
  },
  // 移动端页码按钮的数量端默认值5
  pagerCount: {
    type: Number,
    default: document.body.clientWidth < 992 ? 5 : 7,
  },
  // element 官方配置
  layout: {
    type: String,
    default: 'total, sizes, prev, pager, next, jumper',
  },
  // 是否需要背景色
  background: {
    type: Boolean,
    default: true,
  },
  // 是否分页后触发回滚
  autoScroll: {
    type: Boolean,
    default: true,
  },
  // 是否隐藏
  hidden: {
    type: Boolean,
    default: false,
  },
})

完整代码 html


  <el-table-column
        v-if="isSel && !isRadio"
        type="selection"
        width="55"
        align="center"
        fixed="left"
      />
  <!-- 是否多选 -->
      <el-table-column
        v-if="isSel && !isRadio"
        type="selection"
        width="55"
        align="center"
        fixed="left"
      />
      <!-- 是否单选 -->
   <el-table-column v-if="isRadio" width="70" fixed="left" align="center">
        <template #default="{ row }">
          <el-radio-group
            v-if="row[props.keyId]"
            v-model="tableRadio[props.keyId]"
          >
            <el-radio :label="row[props.keyId]">&nbsp;</el-radio>
          </el-radio-group>
        </template>
      </el-table-column>
         <!-- 是否需要序号 -->
      <el-table-column
        v-if="index"
        label="序号"
        type="index"
        width="70"
        fixed="left"
        align="center"
      />
         <!-- 渲染 逻辑 -->
       <template
        v-for="(columnItem, index) in tableColumn"
        :key="`p-table-${columnItem.prop}-${index}`"
      >
        
        <el-table-column
          :fixed="getFixed(columnItem)"
          :label="columnItem.label"
          :prop="columnItem.prop"
          :rules="columnItem.rules"
          :width="
            columnItem.width !== undefined
              ? columnItem.width
              : columnItem.label.includes('单号') ||
                columnItem.label.includes('物资编码') ||
                columnItem.label.includes('物资名称') ||
                columnItem.label.includes('服务编码') ||
                columnItem.label.includes('服务名称')
              ? '180'
              : columnItem.label == '操作'
              ? '300'
              : ''
          "
          :show-overflow-tooltip="
            columnItem.showText === undefined
              ? isShowText(columnItem)
              : columnItem.showText
          "
        >
          <template #header>
            <div>
              <span style="color: red" v-if="columnItem.required">*</span>
              <span> {{ columnItem.label }}</span>
            </div>
          </template>
          <!--   -->
          <template #default="scope">
            <slot
              name="columnCell"
              :record="scope.row"
              :scope="scope"
              :column="{
                prop: columnItem.prop,
                label: columnItem.label,
                width: columnItem.width,
              }"
              >{{ getData(scope.row, columnItem.prop, columnItem.label) }}</slot
            >
          </template>
        </el-table-column>
      </template>

完整代码 js

<script setup name="p-table">
import { computed } from 'vue'
import Sortable from 'sortablejs'
import { v4 as uuidv4 } from 'uuid'

const { proxy } = getCurrentInstance()

/**
 * 使用此组件 外部需要设置宽度 不能100%宽度 必须写固定宽度 否则会出现宽度无法自适应
 */
const props = defineProps({
  // 列表主键id(必须设置且自动带出)
  keyId: {
    required: false,
    type: String,
    default: 'id',
  },
  // 数据
  data: {
    required: true,
    type: Array,
    default: () => [],
  },
  /**
   *  列表每一行
   * @param {
          label:string, 必填
          prop:string,  必填
          showText:boolean,
          width:string, 非必填
          fixed:string, 非必填
           } column
   */
  column: {
    required: true,
    type: Array,
    default: () => [],
  },
  // 是否展开
  defaultExpandAll: {
    type: Boolean,
    default: false,
  },
  // 拖动class(需要提供本身或上级calss)
  className: {
    type: String,
    default: '',
    required: false,
  },
  //   加载状态
  loading: {
    type: Boolean,
    default: false,
    required: false,
  },
  //   分页总量
  total: {
    type: String || Number,
    default: 0,
    required: false,
  },
  //   高度
  height: {
    type: String,
    default: '40rem',
    required: false,
  },
  //   值为空是 显示
  empty: {
    type: String,
    default: '--',
    required: false,
  },
  //   是否需要序号
  index: {
    type: Boolean,
    default: true,
    required: false,
  },
  //   是否需要复选
  isSel: {
    type: Boolean,
    default: true,
  },
  // 是否需要边框
  border: {
    type: Boolean,
    default: true,
  },
  // 是否需要合计
  summary: {
    type: Boolean,
    default: false,
  },
  // 合计key数组 传入后自动计算
  amountList: {
    type: Array,
    default: () => [],
  },
  // 如果需要自己调整合计 请传入summaryMethodYes true
  summaryMethodYes: {
    type: Boolean,
    default: false,
  },
  // 合计计算 (自调用 带出一个对象值)
  summaryMethod: {
    type: Function,
    default: () => [],
  },
  stripe: {
    type: Boolean,
    default: true,
  },
  // 是否需要树结构
  isTree: {
    type: Boolean,
    default: false,
  },
  // 树结构列表时 子集
  treeProp: {
    type: Object,
    default: {
      children: 'children',
      hasChildren: 'hasChildren',
    },
  },
  // 是否需要单选(单选必须传入keyId,用作判断)
  isRadio: {
    type: Boolean,
    default: false,
  },
  // 是否需要分页
  isPage: {
    type: Boolean,
    default: true,
  },
  // 数据总量
  total: {
    required: true,
    type: Number,
  },
  // 页数
  page: {
    type: Number,
    default: 1,
  },
  // 每页数量
  limit: {
    type: Number,
    default: 20,
  },
  // 默认给出选项每页页数
  pageSizes: {
    type: Array,
    default() {
      return [10, 20, 30, 40, 50, 100, 200, 300, 500]
    },
  },
  // 移动端页码按钮的数量端默认值5
  pagerCount: {
    type: Number,
    default: document.body.clientWidth < 992 ? 5 : 7,
  },
  // element 官方配置
  layout: {
    type: String,
    default: 'total, sizes, prev, pager, next, jumper',
  },
  // 是否需要背景色
  background: {
    type: Boolean,
    default: true,
  },
  // 是否分页后触发回滚
  autoScroll: {
    type: Boolean,
    default: true,
  },
  // 是否隐藏
  hidden: {
    type: Boolean,
    default: false,
  },
})

// 注册事件名称
const emit = defineEmits(['current-change', 'change', 'listChange'])

// 异常说明
const errorMsg = reactive({
  1: '传入数据类型错误,类型应为数组!',
  2: '传入"column"数据错误,prop属性不能重复!',
  3: '当isRadio启用时,必须传入keyId作为唯一标识!',
  4: `当前keyId为${props.keyId},获取不到主键ID,请传入正确keyId`,
})

// 单选时,数据带出
const tableRadio = ref({
  [props.keyId]: '',
})

/**
 * 白名单 判断是否需要 show-overflow-tooltip
 * 一般 操作按钮 开关 标签不需要show-overflow-tooltip 就设置白名单
 * 可设置prop为这些参数,或者设置label同样效果
 */

/**
 *
 */

const whiteList = ref(['x', '操作', '状态'])

// 复选勾选
const selectionChange = (selection, a, b) => {
  try {
    // 判断keyId是否正确
    const resultId = selection.every((t) => t[props.keyId])
    if (!resultId) {
      emit('change', {
        ids: [],
        uids: selection.map((item) => item['uid']),
        index: selection.map((item) => item['index']),
        row: selection,
      })
      throw errorMsg['4']
    }
    emit('change', {
      ids: selection.map((item) => item[props.keyId]),
      index: selection.map((item) => item['index']),
      uids: selection.map((item) => item['uid']),
      row: selection,
    })
  } catch (err) {
    throw 'p-table组件:' + err
  }
}

// 单选勾选
const currentChange = (selection) => {
  try {
    // 点击一行时触发
    emit('current-change', { ids: selection[props.keyId], row: selection })
    // 如果不是单选 禁止 以下操作
    if (!props.isRadio) return
    // 判断keyId是否正确
    if (props.isRadio) {
      if (!selection[props.keyId]) {
        throw errorMsg['3']
      }
    }
    // 给单选框一个值
    tableRadio.value[props.keyId] = selection[props.keyId]
    emit('change', { ids: selection[props.keyId], row: selection })
  } catch (err) {
    throw 'p-table组件:' + err
  }
}

// 筛选 是否有重复 prop
const tableColumn = computed(() => {
  const newData = Array.from(new Set(props.column.map((t) => t.prop)))
  if (props.column.length > newData.length) {
    throw 'p-table组件:' + errorMsg['2']
  } else {
    return props.column
  }
})

// 做一些东西 数据为空的时候做出显示 给予提示
const getData = (val, key, label) => {
  try {
    if (val && key && val[key] != null && val[key] != undefined) {
      return val[key]
    } else {
      if (props.empty) {
        return props.empty
      } else if (!key) {
        return `" ${label} " 的 "prop" 为空`
      } else {
        return `" ${label} "为空, ${key} -> " ${val[key]} "`
      }
    }
  } catch (err) {
    throw 'p-table组件:' + err
  }
}

// 判断是否类型错误
const list = computed(() => {
  if (!Array.isArray(props.data)) {
    throw 'p-table组件:' + errorMsg['1']
  } else {
    // 单选时,数据刷新默认选中一项
    if (props.data.length > 0 && props.isRadio) {
      // 每次置空
      tableRadio.value[props.keyId] = ''
      const obj = props.data[0]
      // 如果为空 抛出异常
      if (!obj[props.keyId]) {
        throw 'p-table组件:' + errorMsg['4']
      }
      // 单选时,默认提供一个唯一标识
      tableRadio.value[props.keyId] = obj[props.keyId]
    }
    // 数据参入uuid
    props.data.forEach((t, ind) => {
      t[`uid`] = uuidv4()
      t['index'] = ind
    })

    return props.data || []
  }
})

// 判断是否需要 show-overflow-tooltip
const isShowText = (val) => {
  if (val.label == '备注') {
    return true
  } else if (val.label == '辅助属性') {
    return false
  } else if (
    whiteList.value.includes(val.label) ||
    whiteList.value.includes(val.prop) ||
    val.prop.length < 2
  ) {
    return false
  } else {
    return true
  }
}

// 判断是否需要 fixed 表格操作栏默认给fixed
const getFixed = (columnItem) => {
  if (columnItem.fixed) {
    return columnItem.fixed
  } else if (columnItem.label == '操作') {
    return 'right'
  } else {
    return false
  }
}

// 合计计算
const getSummaries = (param) => {
  try {
    // summaryMethodYes为true则自己通过 summaryMethod 函数在主页面执行并返回
    if (props.summaryMethodYes) {
      return props.summaryMethod(param)
    } else {
      // 通过 amountList 掺入的数组 自动计算
      if (!Array.isArray(props.amountList))
        return proxy.warningMsg(
          'p-table组件:合计计算数组传递类型错误 ==>' + props.amountList
        )
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计'
          return
        }
        const values = data.map((item) => {
          // 掺入数组对比
          if (props.amountList.includes(column.property)) {
            return Number(item[column.property])
          } else {
            return '-?-'
          }
        })
        if (!values.every((value) => value == '-?-')) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr)
            if (!Number.isNaN(value)) {
              const number = Number(prev) + Number(curr)
              return number.toFixed(2)
            } else {
              return prev
            }
          }, 0)
        } else {
          sums[index] = '/'
        }
      })

      return sums
    }
  } catch (err) {
    throw 'p-table组件:' + err
  }
}

// 提取和赋值分页 页数
const currentPage = computed({
  get() {
    return props.page
  },
  set(val) {
    emit('update:page', val)
  },
})

// 提取和赋值每页数量 每页数量
const pageSize = computed({
  get() {
    return props.limit
  },
  set(val) {
    emit('update:limit', val)
  },
})

// 分页器变化时
function handleSizeChange(val) {
  if (currentPage.value * val > totalCon) {
    currentPage.value = 1
  }
  emit('pagination', { page: currentPage.value, limit: val })
  if (props.autoScroll) {
    scrollTo(0, 800)
  }
}

// 分页器变化时
function handleCurrentChange(val) {
  emit('pagination', { page: val, limit: pageSize.value })
  if (props.autoScroll) {
    scrollTo(0, 800)
  }
}

// 表格拖动

// 创建拖拽实例
const initSort = () => {
  if (!props.className) return
  console.log(props.className, 'props.className')
  const table = document.querySelector(
    `.${props.className} .el-table__body-wrapper tbody`
  )
  Sortable.create(table, {
    group: 'shared',
    animation: 150,
    easing: 'cubic-bezier(1, 0, 0, 1)',
    onStart: () => {},
    // 结束拖动事件
    onEnd: async ({ newIndex, oldIndex }) => {
      setNodeSort(list.value, oldIndex, newIndex)
    },
  })
}

// 拖拽完成修改数据排序
const setNodeSort = (data, oldIndex, newIndex) => {
  const currRow = data.splice(oldIndex, 1)[0]
  data.splice(newIndex, 0, currRow)

  emit('listChange', list.value)
}

// 转int
const totalCon = computed(() => Number(props.total))

onMounted(() => {
  if (props.isTree) return
  initSort()
})
</script>

完整css

<style scoped lang="scss">
.p-table {
  width: 100%;
}
.pagination-container {
  z-index: 22;
  display: flex;
  justify-content: center;
  align-items: center;
  //position: fixed;
  bottom: 1.25rem;
  width: 100%;
}
.fixed {
  position: absolute;
  bottom: 0px;
  height: 2rem;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}
.pagination-container.hidden {
  display: none;
}
#paginationId ::v-deep .el-input__inner {
  width: 100px !important;
}
#paginationId ::v-deep .el-input {
  width: 100px !important;
}
#paginationId ::v-deep .el-select--default {
  width: 100px !important;
}

.tableClass {
  width: 100% !important;
  margin-top: 0.9375rem;
}
</style>