当前位置:首页 > 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实现sso

vue实现sso

Vue 实现 SSO(单点登录)方案 单点登录(SSO)允许用户通过一次登录访问多个相关系统。以下是基于 Vue 的实现方案: 使用 OAuth2/OpenID Connect 协议 OAuth2…

vue实现批量

vue实现批量

Vue 实现批量操作的方法 在 Vue 中实现批量操作通常涉及选择多个项目并执行统一处理,例如批量删除、批量更新等。以下是几种常见实现方式: 表格多选批量操作 使用 el-table 配合复选框实现…

vue实现逻辑

vue实现逻辑

Vue 实现逻辑的核心概念 Vue.js 是一个渐进式 JavaScript 框架,其核心逻辑围绕数据驱动和组件化开发。通过响应式系统、虚拟 DOM 和组件生命周期等机制,Vue 实现了高效的前端开发…

vue实现slot

vue实现slot

Vue 中的 Slot 实现方法 Vue 的 slot 是一种内容分发机制,允许父组件向子组件插入内容。以下是几种常见的 Slot 实现方式: 默认 Slot 子组件通过 <slot>…

vue 实现循环

vue 实现循环

Vue 实现循环的方法 在 Vue 中,可以使用 v-for 指令来实现循环渲染列表数据。以下是几种常见的循环实现方式: 基本数组循环 <template> <ul>…

vue 实现穿透

vue 实现穿透

Vue 样式穿透的实现方法 在 Vue 中,样式穿透通常指在带有 scoped 属性的样式块中,强制影响子组件的样式。以下是几种常见的实现方式: 使用 >>> 或 /deep/ 选…