VUE实现纵向动态表格

下面是一个简单的纵向动态表格示例:

复制代码
<template>
  <div>
    <table>
      <thead>
        <tr>
          <th>名称</th>
          <th v-for="(item, index) in headers" :key="index">{{item}}</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(row, rowIndex) in tableData" :key="rowIndex">
          <td>{{row.name}}</td>
          <td v-for="(item, index) in headers" :key="index">
            <input type="text" v-model="row.data[index]">
          </td>
        </tr>
      </tbody>
    </table>
    <button @click="addRow">添加行</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      headers: ['列1', '列2', '列3'], // 表头
      tableData: [ // 表格内容(可以从后台获取)
        { name: '行1', data: [1, 2, 3] },
        { name: '行2', data: [4, 5, 6] },
        { name: '行3', data: [7, 8, 9] }
      ]
    }
  },
  methods: {
    addRow() { // 添加一行
      this.tableData.push({ name: '新行', data: ['', '', ''] })
    }
  }
}
</script>

在这个示例中,我们使用了一个包含表头和表格内容的数据模型。表格内容是一个数组,其中每个元素都是一个对象,它有一个名称和一个数据数组,数据数组中包含每一列的值。我们使用v-for指令来动态地将表头和表格内容呈现为表格。

我们还为表格添加了一个"添加行"按钮,当它被点击时,我们将向tableData数组中添加一个新行。注意,我们为新行设置了空的数据数组,这样新增行中的每一列都将是空的文本框。

相关推荐
默恋~微凉1 分钟前
shell(八)——WEB与Nginx
开发语言·前端·php
要加油哦~11 分钟前
nrm | npm 的镜像管理工具
前端·npm·node.js·nrm
想不明白的过度思考者11 分钟前
基于 Spring Boot 的 Web 三大核心交互案例精讲
前端·spring boot·后端·交互·javaee
孟祥_成都12 分钟前
不易懂你打我!写给前端和小白的 大模型(ChatGPT) 工作基本原理!
前端·人工智能
恋猫de小郭16 分钟前
回顾 Flutter Flight Plans ,关于 Flutter 的现状和官方热门问题解答
android·前端·flutter
●VON17 分钟前
从零开始:用 Electron 构建你的第一个桌面应用
前端·javascript·electron
艾小码18 分钟前
从源码到npm:手把手带你发布Vue 3组件库
前端·vue.js·npm
张风捷特烈23 分钟前
FlutterUnit3.4.1 | 来场三方库的收录狂欢吧~
android·前端·flutter
t***L2661 小时前
JavaScript在机器学习中的库
开发语言·javascript·机器学习
乔冠宇1 小时前
CSS3中的新增属性总结
前端·javascript·css3