el-table實(shí)現(xiàn)合并表格(每行下方追加備注)

代碼實(shí)現(xiàn)
<template>
    <el-table :data="processedData" :span-method="handleSpan" border @selection-change="handleSelectionChange"
        style="width: 100%">
        <!-- 復(fù)選框列 -->
        <el-table-column type="selection" width="55" align="center" />

        <!-- 圖片列 -->
        <el-table-column label="圖片" width="120" align="center">
            <template #default="{ row }">
                <img v-if="row.type === 'data'" :src="row.img" style="width: 80px; height: 60px; object-fit: cover;" />
            </template>
        </el-table-column>

        <!-- 動態(tài)其他列 -->
        <el-table-column v-for="(col, index) in columns" :key="index" :label="col.label" :prop="col.prop">
            <template #default="{ row }">
                <!-- 數(shù)據(jù)行顯示內(nèi)容 -->
                <span v-if="row.type === 'data'">{{ row[col.prop] }}</span>
                <!-- 備注行顯示備注 -->
                <span v-if="row.type === 'remark'">
                    {{ row.remark }}
                </span>
            </template>
        </el-table-column>
    </el-table>
</template>

<script>
export default {
    data() {
        return {
            // 原始數(shù)據(jù)
            originalData: [
                {
                    id: 1,
                    img: "xxxxx.jpg",
                    name: "張三",
                    age: 30,
                    address: "北京",
                    remark: "這是張三的備注"
                },
                {
                    id: 2,
                    img: "xxxx.jpg",
                    name: "李四",
                    age: 25,
                    address: "上海",
                    remark: "這是李四的備注"
                }
            ],
            processedData: [],
            columns: [
                { label: "姓名", prop: "name" },
                { label: "年齡", prop: "age" },
                { label: "地址", prop: "address" }
            ]
        };
    },
    created() {
        this.processData();
    },
    methods: {
        // 處理數(shù)據(jù):插入備注行
        processData() {
            this.processedData = [];
            this.originalData.forEach(item => {
                // 插入數(shù)據(jù)行
                this.processedData.push({
                    ...item,
                    type: "data"
                });
                // 插入備注行
                this.processedData.push({
                    ...item,
                    type: "remark"
                });
            });
        },

        // 合并單元格邏輯
        handleSpan({ row, column, rowIndex, columnIndex }) {
            if (row.type === 'data') {
                if (columnIndex === 0 || columnIndex === 1) {
                    return { rowspan: 2, colspan: 1 };
                }
            } else if (row.type === 'remark') {
                if (columnIndex === 2) {
                    return {
                        rowspan: 1,
                        colspan: this.columns.length
                    };
                } else {
                    return [0, 0]
                }
            }
        },
        /** 多選框選中數(shù)據(jù) */
        handleSelectionChange(selection) {
            console.log("選中數(shù)據(jù)", selection)
        }
    }
};
</script>
效果圖
image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容