当前位置:首页 > VUE

vue实现输入表格

2026-02-20 11:13:08VUE

Vue 实现输入表格的方法

使用 v-for 动态渲染表格

通过 Vue 的 v-for 指令动态渲染表格行和列,结合 v-model 实现双向数据绑定。

<template>
  <table>
    <thead>
      <tr>
        <th v-for="(header, index) in headers" :key="index">{{ header }}</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="(row, rowIndex) in rows" :key="rowIndex">
        <td v-for="(cell, cellIndex) in row" :key="cellIndex">
          <input v-model="rows[rowIndex][cellIndex]" />
        </td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  data() {
    return {
      headers: ['姓名', '年龄', '职业'],
      rows: [
        ['', '', ''],
        ['', '', '']
      ]
    };
  }
};
</script>

动态添加和删除行

通过方法动态操作 rows 数组,实现表格行的增删功能。

<template>
  <div>
    <button @click="addRow">添加行</button>
    <button @click="removeRow">删除行</button>
    <table>
      <!-- 表格内容同上 -->
    </table>
  </div>
</template>

<script>
export default {
  methods: {
    addRow() {
      this.rows.push(new Array(this.headers.length).fill(''));
    },
    removeRow() {
      if (this.rows.length > 1) {
        this.rows.pop();
      }
    }
  }
};
</script>

表单验证

结合计算属性或第三方库(如 Vuelidate)对输入内容进行验证。

<template>
  <table>
    <tr v-for="(row, rowIndex) in rows" :key="rowIndex">
      <td v-for="(cell, cellIndex) in row" :key="cellIndex">
        <input 
          v-model="rows[rowIndex][cellIndex]" 
          :class="{ 'error': !isValid(rowIndex, cellIndex) }"
        />
      </td>
    </tr>
  </table>
</template>

<script>
export default {
  computed: {
    isValid() {
      return (rowIndex, cellIndex) => {
        return this.rows[rowIndex][cellIndex].trim() !== '';
      };
    }
  }
};
</script>

<style>
.error {
  border: 1px solid red;
}
</style>

使用组件化实现

将表格行或单元格封装为独立组件,提高代码复用性。

vue实现输入表格

<template>
  <table>
    <tbody>
      <table-row 
        v-for="(row, index) in rows" 
        :key="index" 
        :row-data="row"
        @update="updateRow(index, $event)"
      />
    </tbody>
  </table>
</template>

<script>
import TableRow from './TableRow.vue';

export default {
  components: { TableRow },
  data() {
    return { rows: [['张三', '25'], ['李四', '30']] };
  },
  methods: {
    updateRow(index, newRow) {
      this.$set(this.rows, index, newRow);
    }
  }
};
</script>

表格行组件示例

<!-- TableRow.vue -->
<template>
  <tr>
    <td v-for="(cell, index) in rowData" :key="index">
      <input v-model="localRow[index]" @input="onInput" />
    </td>
  </tr>
</template>

<script>
export default {
  props: ['rowData'],
  data() {
    return { localRow: [...this.rowData] };
  },
  methods: {
    onInput() {
      this.$emit('update', this.localRow);
    }
  }
};
</script>

注意事项

  • 使用 v-for 时必须添加 :key 以提高渲染性能
  • 深层数据修改时建议使用 Vue.set 或数组的变异方法
  • 复杂场景可考虑使用 Vuex 管理表格数据状态
  • 移动端适配需额外处理输入框的样式和交互

以上方法可根据实际需求组合使用,例如同时实现动态增删行、表单验证和组件化架构。

标签: 表格vue
分享给朋友:

相关文章

vue按钮实现截屏

vue按钮实现截屏

Vue 按钮实现截屏的方法 在 Vue 项目中实现截屏功能可以通过多种方式完成,以下是几种常见的方法: 使用 html2canvas 库 安装 html2canvas 库: npm install…

vue 实现toast

vue 实现toast

vue 实现 toast 的方法 在 Vue 中实现 Toast 提示功能可以通过多种方式,以下是几种常见的实现方法: 使用第三方库 Vue 生态中有许多成熟的 Toast 库,例如 vue-toa…

vue实现流程

vue实现流程

Vue 实现流程的基本步骤 安装 Vue.js 可以通过 CDN 引入或使用 npm/yarn 安装。 CDN 方式: <script src="https://cdn.jsdelivr.…

vue  实现tab

vue 实现tab

实现 Tab 切换功能 在 Vue 中实现 Tab 切换功能可以通过动态组件或条件渲染完成。以下是两种常见方法: 使用 v-if 或 v-show 实现条件渲染 通过绑定 currentTab 变量…

vue 组件实现

vue 组件实现

Vue 组件实现方法 单文件组件 (SFC) 使用 .vue 文件格式,包含模板、脚本和样式三部分: <template> <div class="example">{{…

vue实现浮标

vue实现浮标

Vue 实现浮动按钮(浮标) 使用 Vue 实现浮动按钮可以通过多种方式完成,以下是几种常见的方法: 使用 CSS 定位和 Vue 组件 创建 Vue 组件并配合 CSS 固定定位实现浮动按钮:…