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
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值