Ant-Design-Vue动态表头并填充数据

使用Ant Design Vue框架的Table组件时,我们通过配置Table组件的colums属性来生成表头

复制代码
<a-table :columns="columns" :data-source="data"></a-table>
const columns = [
  {
    dataIndex: 'name',
    key: 'name',
    slots: { title: 'customTitle' },
    scopedSlots: { customRender: 'name' },
  },
  {
    title: 'Age',
    dataIndex: 'age',
    key: 'age',
  },
  {
    title: 'Address',
    dataIndex: 'address',
    key: 'address',
  },
  {
    title: 'Tags',
    key: 'tags',
    dataIndex: 'tags',
    scopedSlots: { customRender: 'tags' },
  },
  {
    title: 'Action',
    key: 'action',
    scopedSlots: { customRender: 'action' },
  },
];

但是,当我们做智能表单项目的需求是,我们要根据数据库储存的JSON对象动态的生成表头。

当时脑子里的第一个想法是,在拿到后台传过来的JSON数据之后在JS中根据JSON对象创建一个新的colums,然后生成动态的表头。这样做肯定是没有问题的,代码省略。

但是后来采取了循环创建 a-table-column 的方式来实现动态表头。个人感觉这样更符合逻辑一些

复制代码
<a-table>         
     <!-- 定制表格表头 -->
              <template v-for="(item, itemIndex) in widgetData.originalLine.fieldList">
                <a-table-column
                  v-if="hasGrid(item.id)"
                  :title="item.name"
                  :key="item.id"
                  :width="widthCal(item.id)"
                  :align="alignment(item.id)"
                >
                  <template slot-scope="text, record">
                    <div v-if="!item.tableFixedColumnsDisplay">
                      {{ getTableContent(record,item.id) }}
                    </div>
                  </template>
                </a-table-column>
              </template>
复制代码
<a-table>         

这样就搞定了!

相关推荐
十步杀一人_千里不留行9 小时前
Google 登录集成教程(Web + Expo 移动端)
前端
gAlAxy...12 小时前
IntelliJ IDEA 四种项目构建:从普通 Java 到 Maven Web 项目
前端·firefox
my一阁12 小时前
2025-web集群-问题总结
前端·arm开发·数据库·nginx·负载均衡·web
会飞的小妖12 小时前
个人博客系统(十一、前端-简短的配置)
前端
念念不忘 必有回响14 小时前
nginx前端部署与Vite环境变量配置指南
前端·nginx·vite
JIngJaneIL14 小时前
篮球论坛|基于SprinBoot+vue的篮球论坛系统(源码+数据库+文档)
java·前端·数据库·vue.js·论文·毕设·篮球论坛系统
程序猿阿伟16 小时前
《首屏加载优化手册:Vue3+Element Plus项目提速的技术细节》
前端·javascript·vue.js
麦麦大数据16 小时前
D030知识图谱科研文献论文推荐系统vue+django+Neo4j的知识图谱|论文本文相似度推荐|协同过滤
vue.js·爬虫·django·知识图谱·科研·论文文献·相似度推荐
尘觉16 小时前
面试-浅复制和深复制?怎样实现深复制详细解答
javascript·面试·职场和发展
fruge17 小时前
Vue Pinia 状态管理实战指南
前端·vue.js·ubuntu