el-table动态表头与模拟请求数据

html 复制代码
<el-table
            :data="tableData" style="width: 100%"
        >
            <el-table-column
                v-for="column in dynamicColumns"
                :key="column.prop"
                :prop="column.prop"
                :label="column.label"
                :width="column.width"
            >
                <template #default="{row, column, $index}">
                    <span v-if="column.label === '对比列'">{{ row[column.prop] }}</span>
                    <template v-else>
                        <div class="image-description">
                            <img
                                :src="row.imageAndDescription.imageUrl"
                                alt="Image"
                                style="max-width: 100%;
                                max-height: 100px;"
                            >
                            <div>{{ row.imageAndDescription.description }}</div>
                        </div>
                    </template>
                </template>
            </el-table-column>

            <!-- <el-table-column
                prop="date" label="对比列"
                width="180"
            />
            <el-table-column
                prop="name" label="标注任务1"
                width="180"
            />
            <el-table-column prop="address" label="标注任务2"/> -->
            <!-- <el-table-column label="动态列" width="100">
                <template v-slot="scope">
                    {{ dynamicHeaderData }}
                </template>
            </el-table-column> -->
        </el-table>

模拟请求数据

js 复制代码
const tableData = ref([]);
// 从接口获取表头数据
const dynamicColumns = ref([]);

onMounted(async () => {
    try {
        const res = await fetchDataFromApi();
        dynamicColumns.value = res.data.columns;
        tableData.value = res.data.tableData;
    }
    catch (error) {
        console.error('Error fetching data', error);
    }
});

// 模拟一个获取数据的函数
function fetchDataFromApi() {
    return new Promise(resolve => {
        // 模拟接口响应
        setTimeout(() => {
            resolve({
                data: {
                    columns: [
                        {prop: 'title', label: '对比列', width: '180'},
                        {prop: 'imageAndDescription', label: '标注任务1', width: '300'},
                        {prop: 'imageAndDescription', label: '标注任务2', width: '300'},
                        {prop: 'imageAndDescription', label: '标注任务3'},
                    ],
                    tableData: [
                        {title: '狗', imageAndDescription: {imageUrl: 'https://example.com/dog.jpg', description: '这只狗很可爱,高清图。'}},
                        {title: '猫', imageAndDescription: {imageUrl: 'https://example.com/cat.jpg', description: '这只猫也很可爱,高清图。'}},
                        {title: '猫', imageAndDescription: {imageUrl: 'https://example.com/cat.jpg', description: '这只猫也很可爱,高清图。'}},
                        {title: '猫', imageAndDescription: {imageUrl: 'https://example.com/cat.jpg', description: '这只猫也很可爱,高清图。'}},
                    ],
                },
            });
        }, 1000);
    });
}

// const dynamicColumns = ref([
//     {prop: 'date', label: '对比列', width: '180'},
//     {prop: 'name', label: '标注任务1', width: '180'},
//     {prop: 'address', label: '标注任务2'},
// ]);
相关推荐
zqx_711 分钟前
随记 前端框架React的初步认识
前端·react.js·前端框架
惜.己28 分钟前
javaScript基础(8个案例+代码+效果图)
开发语言·前端·javascript·vscode·css3·html5
什么鬼昵称1 小时前
Pikachu-csrf-CSRF(get)
前端·csrf
长天一色1 小时前
【ECMAScript 从入门到进阶教程】第三部分:高级主题(高级函数与范式,元编程,正则表达式,性能优化)
服务器·开发语言·前端·javascript·性能优化·ecmascript
NiNg_1_2341 小时前
npm、yarn、pnpm之间的区别
前端·npm·node.js
秋殇与星河1 小时前
CSS总结
前端·css
NiNg_1_2341 小时前
Vue3 Pinia持久化存储
开发语言·javascript·ecmascript
读心悦1 小时前
如何在 Axios 中封装事件中心EventEmitter
javascript·http
BigYe程普2 小时前
我开发了一个出海全栈SaaS工具,还写了一套全栈开发教程
开发语言·前端·chrome·chatgpt·reactjs·个人开发
神之王楠2 小时前
如何通过js加载css和html
javascript·css·html