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>         

这样就搞定了!

相关推荐
哆啦A梦158812 分钟前
[前台小程序] 01 项目初始化
前端·vue.js·uni-app
智码看视界1 小时前
老梁聊全栈系列:(阶段一)架构思维与全局观
java·javascript·架构
小周同学@3 小时前
谈谈对this的理解
开发语言·前端·javascript
Wiktok3 小时前
Pyside6加载本地html文件并实现与Javascript进行通信
前端·javascript·html·pyside6
一只小风华~3 小时前
Vue:条件渲染 (Conditional Rendering)
前端·javascript·vue.js·typescript·前端框架
柯南二号3 小时前
【大前端】前端生成二维码
前端·二维码
程序员码歌3 小时前
明年35岁了,如何破局?说说心里话
android·前端·后端
博客zhu虎康4 小时前
React Hooks 报错?一招解决useState问题
前端·javascript·react.js
灰海4 小时前
vue中通过heatmap.js实现热力图(多个热力点)热区展示(带鼠标移入弹窗)
前端·javascript·vue.js·heatmap·heatmapjs
王源骏5 小时前
LayaAir鼠标(手指)控制相机旋转,限制角度
前端