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. VUE实现纵向动态表格

    2023-12-07 14:08:06       42 阅读
  2. 使用 Ant Design Vue 实现动态表头与数据填充

    2023-12-07 14:08:06       10 阅读
  3. el-table 纵向垂直表头

    2023-12-07 14:08:06       40 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-07 14:08:06       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-07 14:08:06       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-07 14:08:06       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-07 14:08:06       20 阅读

热门阅读

  1. Redis配置项汇总(chao详细)

    2023-12-07 14:08:06       35 阅读
  2. Hadoop学习笔记(HDP)-Part.05 Yum源配置

    2023-12-07 14:08:06       36 阅读
  3. 如何二次封装一个Vue3组件库?

    2023-12-07 14:08:06       36 阅读