当前位置:首页 > VUE

vue实现用户信息表

2026-02-09 18:31:13VUE

Vue 实现用户信息表

数据准备

在 Vue 组件的 datasetup 中定义用户信息数据。通常使用数组存储多个用户信息,每个用户是一个对象。

data() {
  return {
    users: [
      { id: 1, name: '张三', age: 25, email: 'zhangsan@example.com' },
      { id: 2, name: '李四', age: 30, email: 'lisi@example.com' }
    ]
  }
}

表格渲染

使用 v-for 指令循环渲染用户信息到表格中。表格结构包含表头和表体,表体通过 v-for 动态生成。

<table>
  <thead>
    <tr>
      <th>ID</th>
      <th>姓名</th>
      <th>年龄</th>
      <th>邮箱</th>
    </tr>
  </thead>
  <tbody>
    <tr v-for="user in users" :key="user.id">
      <td>{{ user.id }}</td>
      <td>{{ user.name }}</td>
      <td>{{ user.age }}</td>
      <td>{{ user.email }}</td>
    </tr>
  </tbody>
</table>

添加样式

为表格添加基础样式,提升可读性和美观性。可以使用内联样式或外部 CSS 文件。

vue实现用户信息表

table {
  width: 100%;
  border-collapse: collapse;
}
th, td {
  border: 1px solid #ddd;
  padding: 8px;
  text-align: left;
}
th {
  background-color: #f2f2f2;
}

动态操作

实现添加、删除或编辑用户信息的功能。通过方法更新 users 数组,Vue 会自动重新渲染表格。

methods: {
  addUser() {
    this.users.push({
      id: this.users.length + 1,
      name: '新用户',
      age: 0,
      email: 'new@example.com'
    });
  },
  deleteUser(id) {
    this.users = this.users.filter(user => user.id !== id);
  }
}

表单绑定

通过 v-model 实现表单与数据的双向绑定,方便用户输入新信息或编辑现有信息。

vue实现用户信息表

<input v-model="newUser.name" placeholder="姓名">
<input v-model="newUser.age" placeholder="年龄" type="number">
<input v-model="newUser.email" placeholder="邮箱">
<button @click="addUser">添加用户</button>

完整示例

结合上述内容,完整的 Vue 组件示例如下:

<template>
  <div>
    <table>
      <thead>
        <tr>
          <th>ID</th>
          <th>姓名</th>
          <th>年龄</th>
          <th>邮箱</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="user in users" :key="user.id">
          <td>{{ user.id }}</td>
          <td>{{ user.name }}</td>
          <td>{{ user.age }}</td>
          <td>{{ user.email }}</td>
          <td><button @click="deleteUser(user.id)">删除</button></td>
        </tr>
      </tbody>
    </table>
    <div>
      <input v-model="newUser.name" placeholder="姓名">
      <input v-model="newUser.age" placeholder="年龄" type="number">
      <input v-model="newUser.email" placeholder="邮箱">
      <button @click="addUser">添加用户</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      users: [
        { id: 1, name: '张三', age: 25, email: 'zhangsan@example.com' },
        { id: 2, name: '李四', age: 30, email: 'lisi@example.com' }
      ],
      newUser: {
        name: '',
        age: 0,
        email: ''
      }
    }
  },
  methods: {
    addUser() {
      this.users.push({
        id: this.users.length + 1,
        ...this.newUser
      });
      this.newUser = { name: '', age: 0, email: '' };
    },
    deleteUser(id) {
      this.users = this.users.filter(user => user.id !== id);
    }
  }
}
</script>

<style>
table {
  width: 100%;
  border-collapse: collapse;
}
th, td {
  border: 1px solid #ddd;
  padding: 8px;
  text-align: left;
}
th {
  background-color: #f2f2f2;
}
</style>

进阶功能

可以根据需求扩展更多功能,如分页、排序、筛选等。使用计算属性或第三方库(如 Lodash)简化复杂操作。

computed: {
  sortedUsers() {
    return [...this.users].sort((a, b) => a.age - b.age);
  }
}

标签: 用户信息vue
分享给朋友:

相关文章

vue实现矩阵

vue实现矩阵

Vue 实现矩阵的方法 在 Vue 中实现矩阵操作可以通过多种方式完成,以下是一些常见的方法: 使用二维数组表示矩阵 矩阵可以用二维数组来表示,每个元素对应矩阵中的一个值。在 Vue 的 data…

vue实现app

vue实现app

Vue 实现 App 的常见方法 使用 Vue 开发移动端 App 主要有两种主流方案:混合开发(Hybrid)和原生渲染。以下是具体实现方式和工具链: 混合开发方案(WebView 嵌入) 通…

vue实现treeselect

vue实现treeselect

Vue TreeSelect 实现方法 使用 Vue 实现 TreeSelect 组件可以通过以下几种方式完成,包括使用现成的第三方库或自行封装组件。 使用第三方库 推荐使用成熟的第三方 Tree…

vue实现gps

vue实现gps

Vue 中实现 GPS 定位功能 在 Vue 中实现 GPS 定位功能通常依赖于浏览器的 Geolocation API 或第三方地图服务(如高德、百度地图等)。以下是两种常见的实现方式: 使用浏…

vue实现录像

vue实现录像

Vue 实现录像功能 在 Vue 中实现录像功能通常需要借助浏览器的 MediaDevices API 和 MediaRecorder API。以下是实现步骤: 获取用户摄像头和麦克风权限 使用…

vue cli 实现

vue cli 实现

Vue CLI 实现步骤 安装 Vue CLI 确保 Node.js 版本在 8.9 或以上。通过 npm 全局安装 Vue CLI: npm install -g @vue/cli 创建新项目 使…