Vue使用Antd中a-table實(shí)現(xiàn)表格數(shù)據(jù)列合并展示示例代碼
原數(shù)據(jù)
根據(jù)需求實(shí)現(xiàn)當(dāng)前兩列數(shù)據(jù)中有相同數(shù)據(jù)時(shí),合并列單元格

實(shí)現(xiàn)

源碼
數(shù)據(jù)
const dataSource = ref([
{
id: 1,
pl: "冰箱",
zznd: "P1",
sm: "說(shuō)明說(shuō)明說(shuō)明1",
dw: "臺(tái)",
gs: "1",
dj: "100"
},
{
id: 1,
pl: "冰箱",
zznd: "P2",
sm: "說(shuō)明說(shuō)明說(shuō)明2",
dw: "臺(tái)",
gs: "1",
dj: "100"
},
{
id: 1,
pl: "冰箱",
zznd: "P2",
sm: "說(shuō)明說(shuō)明說(shuō)明3",
dw: "臺(tái)",
gs: "1",
dj: "100"
},
{
id: 1,
pl: "冰箱",
zznd: "P3",
sm: "說(shuō)明說(shuō)明說(shuō)明4",
dw: "臺(tái)",
gs: "1",
dj: "100"
},
{
id: 1,
pl: "冰箱",
zznd: "P3",
sm: "說(shuō)明說(shuō)明說(shuō)明4",
dw: "臺(tái)",
gs: "1",
dj: "100"
}
])處理函數(shù)
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: "說(shuō)明說(shuō)明說(shuō)明1",
dw: "臺(tái)",
gs: "1",
dj: "100"
},
{
id: 1,
pl: "冰箱",
zznd: "P2",
sm: "說(shuō)明說(shuō)明說(shuō)明2",
dw: "臺(tái)",
gs: "1",
dj: "100"
},
{
id: 1,
pl: "冰箱",
zznd: "P2",
sm: "說(shuō)明說(shuō)明說(shuō)明3",
dw: "臺(tái)",
gs: "1",
dj: "100"
},
{
id: 1,
pl: "冰箱",
zznd: "P3",
sm: "說(shuō)明說(shuō)明說(shuō)明4",
dw: "臺(tái)",
gs: "1",
dj: "100"
},
{
id: 1,
pl: "冰箱",
zznd: "P3",
sm: "說(shuō)明說(shuō)明說(shuō)明4",
dw: "臺(tái)",
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: "品類(lèi)",
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: "說(shuō)明",
dataIndex: "sm",
width: 180
},
{
title: "單位(臺(tái)/件)",
dataIndex: "dw",
width: 180
},
{
title: "工時(shí)(小時(shí))",
dataIndex: "gs",
width: 180
},
{
title: "單價(jià)(元)",
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>到此這篇關(guān)于Vue中使用Antd中a-table實(shí)現(xiàn)表格數(shù)據(jù)列合并展示的文章就介紹到這了,更多相關(guān)vue Antd內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在elementui中Notification組件添加點(diǎn)擊事件實(shí)例
這篇文章主要介紹了在elementui中Notification組件添加點(diǎn)擊事件實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11
vue3集成Element-plus實(shí)現(xiàn)按需自動(dòng)引入組件的方法總結(jié)
vue3出來(lái)一段時(shí)間了,element也更新了版本去兼容vue3,下面這篇文章主要給大家介紹了關(guān)于vue3集成Element-plus實(shí)現(xiàn)按需自動(dòng)引入組件的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
手寫(xiě)Vue源碼之?dāng)?shù)據(jù)劫持示例詳解
這篇文章主要給大家介紹了手寫(xiě)Vue源碼之?dāng)?shù)據(jù)劫持的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
Vue 實(shí)現(xiàn)顯示/隱藏層的思路(加全局點(diǎn)擊事件)
這篇文章主要介紹了Vue 實(shí)現(xiàn)顯示/隱藏層的思路(加全局點(diǎn)擊事件),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12
vue使用this.$message不生效的部分原因及解決方案
這篇文章主要介紹了vue使用this.$message不生效的部分原因及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
vue3的setup語(yǔ)法如何自定義v-model為公用hooks
這篇文章主要介紹了vue3的setup語(yǔ)法如何自定義v-model為公用hooks,文章分為兩個(gè)部分介紹,簡(jiǎn)單介紹vue3的setup語(yǔ)法如何自定義v-model和如何提取v-model語(yǔ)法作為一個(gè)公用hooks2022-07-07
運(yùn)行npm?run?dev報(bào)錯(cuò)的原因及解決
剛剛創(chuàng)建好vue項(xiàng)目的時(shí)候,運(yùn)行 npm run dev 會(huì)報(bào)錯(cuò),下面這篇文章主要給大家介紹了關(guān)于運(yùn)行npm?run?dev報(bào)錯(cuò)的原因及解決方法,需要的朋友可以參考下2022-10-10
vue使用路由守衛(wèi)實(shí)現(xiàn)菜單的權(quán)限設(shè)置
我們使?vue-element-admin前端框架開(kāi)發(fā)后臺(tái)管理系統(tǒng)時(shí),?般都會(huì)涉及到菜單的權(quán)限控制問(wèn)題,下面這篇文章主要給大家介紹了關(guān)于vue使用路由守衛(wèi)實(shí)現(xiàn)菜單的權(quán)限設(shè)置的相關(guān)資料,需要的朋友可以參考下2023-06-06

