当前位置: 代码网 > it编程>编程语言>Javascript > Vue使用Antd中a-table实现表格数据列合并展示示例代码

Vue使用Antd中a-table实现表格数据列合并展示示例代码

2024年11月25日 Javascript 我要评论
原数据根据需求实现当前两列数据中有相同数据时,合并列单元格实现源码数据const datasource = ref([ { id: 1, pl: "冰箱", zznd: "p1",

原数据

根据需求实现当前两列数据中有相同数据时,合并列单元格

实现

源码

数据

const datasource = ref([
  {
    id: 1,
    pl: "冰箱",
    zznd: "p1",
    sm: "说明说明说明1",
    dw: "台",
    gs: "1",
    dj: "100"
  },
  {
    id: 1,
    pl: "冰箱",
    zznd: "p2",
    sm: "说明说明说明2",
    dw: "台",
    gs: "1",
    dj: "100"
  },
  {
    id: 1,
    pl: "冰箱",
    zznd: "p2",
    sm: "说明说明说明3",
    dw: "台",
    gs: "1",
    dj: "100"
  },
  {
    id: 1,
    pl: "冰箱",
    zznd: "p3",
    sm: "说明说明说明4",
    dw: "台",
    gs: "1",
    dj: "100"
  },
  {
    id: 1,
    pl: "冰箱",
    zznd: "p3",
    sm: "说明说明说明4",
    dw: "台",
    gs: "1",
    dj: "100"
  }
])

处理函数

const calculaterowspan = (data, index, key) => {
  if (index === 0 || data[index][key] !== data[index - 1][key]) {
    let rowspan = 1
    for (let i = index + 1; i < data.length; i++) {
      if (data[i][key] === data[index][key]) {
        rowspan++
      } else {
        break
      }
    }
    return rowspan
  }
  return 0
}

 全部源码

<template>
  <a-modal
    title=" "
    :footer="null"
    width="1160px"
    wrap-class-name="price-modal-wrap"
    :open="open"
    @update:open="submit"
    centered
    destroy-on-close>
    <!-- @update:open="(v: boolean) => emit('update:open', v)" -->
    <div class="px-[54px] pb-[30px] pt-3">
      <div class="flex flex-col items-center">
        <img src="@/assets/images/steptemp/success.svg" alt="" />
        <span class="text-[#000] text-[24px] font-medium mb-[12px]">提交成功</span>
      </div>
      <a-table bordered :loading="loading" :columns="columns" :data-source="datasource" :pagination="false">
        <template #bodycell="{ text, column, record, index }">
          <template v-if="!['demandid', 'createtime', 'operator'].includes(string(column.dataindex))">
            <a-tooltip placement="topleft" :title="text">
              {{ text }}
            </a-tooltip>
          </template>
        </template>
      </a-table>
      <div class="text-right pt-5">
        <a-button type="primary" class="w-[120px]" @click="submit">确定</a-button>
      </div>
    </div>
  </a-modal>
</template>
<script setup lang="ts">
const props = defineprops<{
  open: boolean
  flag?: string
}>()
const emit = defineemits(["update:open", "goback"])
const submit = () => {
  if (props.flag === "table") {
    emit("update:open", false)
    return
  }
  emit("goback")
}
const datasource = ref([
  {
    id: 1,
    pl: "冰箱",
    zznd: "p1",
    sm: "说明说明说明1",
    dw: "台",
    gs: "1",
    dj: "100"
  },
  {
    id: 1,
    pl: "冰箱",
    zznd: "p2",
    sm: "说明说明说明2",
    dw: "台",
    gs: "1",
    dj: "100"
  },
  {
    id: 1,
    pl: "冰箱",
    zznd: "p2",
    sm: "说明说明说明3",
    dw: "台",
    gs: "1",
    dj: "100"
  },
  {
    id: 1,
    pl: "冰箱",
    zznd: "p3",
    sm: "说明说明说明4",
    dw: "台",
    gs: "1",
    dj: "100"
  },
  {
    id: 1,
    pl: "冰箱",
    zznd: "p3",
    sm: "说明说明说明4",
    dw: "台",
    gs: "1",
    dj: "100"
  }
])
const loading = ref(false)
const calculaterowspan = (data, index, key) => {
  if (index === 0 || data[index][key] !== data[index - 1][key]) {
    let rowspan = 1
    for (let i = index + 1; i < data.length; i++) {
      if (data[i][key] === data[index][key]) {
        rowspan++
      } else {
        break
      }
    }
    return rowspan
  }
  return 0
}
const columns = [
  {
    title: "品类",
    dataindex: "pl",
    width: 180,
    customcell: (_, index) => {
      const rowspan = calculaterowspan(datasource.value, index, "pl")
      return {
        rowspan
      }
    }
  },
  {
    title: "制作难度",
    dataindex: "zznd",
    ellipsis: true,
    width: 180,
    customcell: (_, index) => {
      const rowspan = calculaterowspan(datasource.value, index, "zznd")
      return {
        rowspan
      }
    }
  },
  {
    title: "说明",
    dataindex: "sm",
    width: 180
  },
  {
    title: "单位(台/件)",
    dataindex: "dw",
    width: 180
  },
  {
    title: "工时(小时)",
    dataindex: "gs",
    width: 180
  },
  {
    title: "单价(元)",
    dataindex: "dj",
    width: 180
  }
  // {
  //   title: "操作",
  //   dataindex: "operator",
  //   width: 140
  // }
]
</script>
<style lang="scss">
.price-modal-wrap {
  .ant-modal-content {
    @apply rounded-[20px] overflow-hidden;
  }
  .ant-modal-header {
    @apply rounded-[20px_20px_0_0] py-10 px-[30px] border-none;
  }
  .ant-modal-title {
    @apply text-[20px] font-medium leading-[22px];
  }
  .ant-modal-body {
    padding: 0;
  }
  .ant-modal-close {
    @apply right-[30px] top-[42px];
  }
}
</style>

到此这篇关于vue中使用antd中a-table实现表格数据列合并展示的文章就介绍到这了,更多相关vue antd内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com