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>         

这样就搞定了!

相关推荐
我不吃饼干4 小时前
在 React 中实现倒计时功能会有什么坑
前端·react.js
小小小小宇4 小时前
前端PerformanceObserver
前端
王者鳜錸4 小时前
PYTHON从入门到实践-18Django从零开始构建Web应用
前端·python·sqlite
拾光拾趣录4 小时前
ES6到HTTPS全链路连环拷问,99%人第3题就翻车?
前端·面试
haaaaaaarry5 小时前
Element Plus常见基础组件(二)
开发语言·前端·javascript
xyphf_和派孔明6 小时前
关于echarts的性能优化考虑
前端·性能优化·echarts
PyHaVolask6 小时前
HTML 表单进阶:用户体验优化与实战应用
前端·javascript·html·用户体验
A了LONE6 小时前
cv弹窗,退款确认弹窗
java·服务器·前端
AntBlack7 小时前
闲谈 :AI 生成视频哪家强 ,掘友们有没有推荐的工具?
前端·后端·aigc
花菜会噎住7 小时前
Vue3核心语法进阶(computed与监听)
前端·javascript·vue.js