当前位置:首页 > VUE

在vue实现学生表格

2026-01-12 06:14:01VUE

实现学生表格的基本结构

在Vue中实现学生表格需要使用<table>标签结合Vue的数据绑定功能。准备一个数组存储学生数据,通过v-for指令动态渲染表格行。

<template>
  <div>
    <table>
      <thead>
        <tr>
          <th>学号</th>
          <th>姓名</th>
          <th>年龄</th>
          <th>成绩</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="student in students" :key="student.id">
          <td>{{ student.id }}</td>
          <td>{{ student.name }}</td>
          <td>{{ student.age }}</td>
          <td>{{ student.score }}</td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      students: [
        { id: 1, name: '张三', age: 18, score: 90 },
        { id: 2, name: '李四', age: 19, score: 85 },
        { id: 3, name: '王五', age: 20, score: 92 }
      ]
    }
  }
}
</script>

添加表格样式

为表格添加基础CSS样式可以提升视觉效果。使用scoped样式确保样式只作用于当前组件。

<style scoped>
table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 20px;
}
th, td {
  border: 1px solid #ddd;
  padding: 8px;
  text-align: left;
}
th {
  background-color: #f2f2f2;
}
tr:nth-child(even) {
  background-color: #f9f9f9;
}
tr:hover {
  background-color: #e9e9e9;
}
</style>

实现动态增删功能

通过方法控制学生数据的增删操作,添加对应的按钮和事件处理。

<template>
  <div>
    <button @click="addStudent">添加学生</button>
    <table>
      <!-- 表头省略 -->
      <tbody>
        <tr v-for="(student, index) in students" :key="student.id">
          <!-- 单元格省略 -->
          <td>
            <button @click="deleteStudent(index)">删除</button>
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
export default {
  methods: {
    addStudent() {
      const newId = this.students.length + 1
      this.students.push({
        id: newId,
        name: `新学生${newId}`,
        age: 18,
        score: Math.floor(Math.random() * 50) + 50
      })
    },
    deleteStudent(index) {
      this.students.splice(index, 1)
    }
  }
}
</script>

添加排序功能

实现按列排序功能需要添加排序方法和排序状态管理。

<template>
  <div>
    <table>
      <thead>
        <tr>
          <th @click="sortBy('id')">学号</th>
          <th @click="sortBy('name')">姓名</th>
          <th @click="sortBy('age')">年龄</th>
          <th @click="sortBy('score')">成绩</th>
        </tr>
      </thead>
      <!-- 表格内容省略 -->
    </table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      sortKey: '',
      sortOrder: 1
    }
  },
  methods: {
    sortBy(key) {
      if (this.sortKey === key) {
        this.sortOrder *= -1
      } else {
        this.sortKey = key
        this.sortOrder = 1
      }
      this.students.sort((a, b) => {
        if (a[key] < b[key]) return -1 * this.sortOrder
        if (a[key] > b[key]) return 1 * this.sortOrder
        return 0
      })
    }
  }
}
</script>

实现分页功能

对于大量数据,添加分页功能可以提升用户体验。

<template>
  <div>
    <table><!-- 表格内容省略 --></table>
    <div class="pagination">
      <button @click="prevPage" :disabled="currentPage === 1">上一页</button>
      <span>第 {{ currentPage }} 页</span>
      <button @click="nextPage" :disabled="currentPage === totalPages">下一页</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentPage: 1,
      pageSize: 5
    }
  },
  computed: {
    paginatedStudents() {
      const start = (this.currentPage - 1) * this.pageSize
      return this.students.slice(start, start + this.pageSize)
    },
    totalPages() {
      return Math.ceil(this.students.length / this.pageSize)
    }
  },
  methods: {
    prevPage() {
      if (this.currentPage > 1) this.currentPage--
    },
    nextPage() {
      if (this.currentPage < this.totalPages) this.currentPage++
    }
  }
}
</script>

在vue实现学生表格

标签: 表格学生
分享给朋友:

相关文章

用css制作表格

用css制作表格

使用CSS制作表格 CSS可以用来样式化HTML表格,使其更具视觉吸引力和功能性。以下是几种常见的方法: 基础表格样式 通过CSS可以调整表格的边框、间距和颜色: <table>…

vue 表格实现

vue 表格实现

Vue 表格实现方法 基础表格实现 使用 el-table 组件(Element UI)实现基础表格: <template> <el-table :data="tableDa…

css表格制作表格

css表格制作表格

CSS 表格制作方法 基础表格结构 使用HTML的<table>标签创建表格框架,搭配<tr>(行)、<td>(单元格)和<th>(表头)标签: &l…

h5实现表格样式

h5实现表格样式

实现H5表格样式的方法 使用HTML和CSS创建基础表格 在HTML中,表格通过<table>标签定义,配合<tr>(行)、<td>(单元格)和<th&g…

vue element实现表格

vue element实现表格

Vue + Element UI 实现表格 Element UI 提供了强大的表格组件 el-table,结合 Vue 可以快速实现数据展示、排序、筛选等功能。 基础表格实现 安装 Element…

vue实现简单表格

vue实现简单表格

使用 Vue 实现简单表格 Vue 提供了多种方式实现表格功能,以下是基于 Vue 3 的两种常见方法: 基础表格实现 通过 v-for 指令循环渲染表格数据: <template>…