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>         

这样就搞定了!

相关推荐
Jagger_1 小时前
抱怨到躺床关灯的一次 DIY 记录
前端
陈随易4 小时前
前端大咖mizchi不满Rust、TypeScript却爱上MoonBit
前端·后端·程序员
whinc5 小时前
🚀 两年小程序开发,我把踩过的坑做成了开源 Skills
前端·微信小程序·ai编程
兆子龙6 小时前
ahooks useRequest 深度解析:一个 Hook 搞定所有请求
java·javascript
兆子龙6 小时前
React Suspense 从入门到实战:让异步加载更优雅
java·javascript
sure2826 小时前
React Native中创建自定义渐变色
前端·react native
KKKK7 小时前
SSE(Server-Sent Events)流式传输原理和XStream实践
前端·javascript
子兮曰7 小时前
Humanizer-zh 实战:把 AI 初稿改成“能发布”的技术文章
前端·javascript·后端
Din8 小时前
主动取消的防抖
前端·javascript·typescript