当前位置:首页 > VUE

vue实现成绩显示

2026-01-22 15:41:43VUE

Vue 实现成绩显示功能

数据准备

在 Vue 中创建一个数组或对象来存储成绩数据,可以包含学生姓名、科目、分数等信息。例如:

data() {
  return {
    students: [
      { name: '张三', math: 85, english: 90, science: 78 },
      { name: '李四', math: 92, english: 88, science: 85 },
      { name: '王五', math: 78, english: 82, science: 90 }
    ]
  }
}

模板渲染

使用 Vue 的模板语法 v-for 循环遍历成绩数据,并在表格中显示:

<table>
  <thead>
    <tr>
      <th>姓名</th>
      <th>数学</th>
      <th>英语</th>
      <th>科学</th>
    </tr>
  </thead>
  <tbody>
    <tr v-for="student in students" :key="student.name">
      <td>{{ student.name }}</td>
      <td>{{ student.math }}</td>
      <td>{{ student.english }}</td>
      <td>{{ student.science }}</td>
    </tr>
  </tbody>
</table>

计算属性

如果需要计算总分或平均分,可以使用 Vue 的计算属性:

computed: {
  studentsWithTotal() {
    return this.students.map(student => {
      return {
        ...student,
        total: student.math + student.english + student.science,
        average: (student.math + student.english + student.science) / 3
      }
    })
  }
}

样式优化

通过 Vue 的 :class:style 动态绑定样式,例如根据分数高低显示不同颜色:

<td :style="{ color: student.math >= 90 ? 'green' : student.math >= 60 ? 'black' : 'red' }">
  {{ student.math }}
</td>

组件化

如果成绩显示功能较复杂,可以将其拆分为单独的组件:

Vue.component('grade-table', {
  props: ['students'],
  template: `
    <table>
      <!-- 表格内容 -->
    </table>
  `
})

数据交互

通过 API 获取成绩数据,使用 axiosfetch

methods: {
  async fetchGrades() {
    try {
      const response = await axios.get('/api/grades')
      this.students = response.data
    } catch (error) {
      console.error('获取成绩失败:', error)
    }
  }
},
created() {
  this.fetchGrades()
}

过滤与排序

添加过滤或排序功能,例如按总分排序:

computed: {
  sortedStudents() {
    return [...this.students].sort((a, b) => {
      const totalA = a.math + a.english + a.science
      const totalB = b.math + b.english + b.science
      return totalB - totalA
    })
  }
}

响应式更新

使用 Vue 的响应式特性,当成绩数据变化时自动更新界面:

methods: {
  updateGrade(name, subject, score) {
    const student = this.students.find(s => s.name === name)
    if (student) {
      Vue.set(student, subject, score)
    }
  }
}

完整示例

将以上功能整合到一个完整的 Vue 实例中:

vue实现成绩显示

new Vue({
  el: '#app',
  data() {
    return {
      students: []
    }
  },
  computed: {
    sortedStudents() {
      return [...this.students].sort((a, b) => {
        const totalA = a.math + a.english + a.science
        const totalB = b.math + b.english + b.science
        return totalB - totalA
      })
    }
  },
  methods: {
    async fetchGrades() {
      try {
        const response = await axios.get('/api/grades')
        this.students = response.data
      } catch (error) {
        console.error('获取成绩失败:', error)
      }
    }
  },
  created() {
    this.fetchGrades()
  }
})

标签: 成绩vue
分享给朋友:

相关文章

vue实现模块

vue实现模块

Vue 实现模块化的方法 Vue 支持多种模块化开发方式,可以根据项目需求选择适合的方案。 使用单文件组件(SFC) 单文件组件是 Vue 最常用的模块化方式,将模板、脚本和样式封装在一个 .vue…

vue实现标题

vue实现标题

Vue 实现标题的方法 在Vue中实现标题可以通过多种方式,以下是几种常见的方法: 方法一:使用模板语法 在Vue组件的模板中直接使用HTML的<h1>到<h6>标签来定义…

vue实现快手

vue实现快手

Vue 实现类似快手的功能 Vue.js 是一个渐进式 JavaScript 框架,适合构建复杂的单页应用。要实现类似快手的功能,可以结合 Vue 和相关技术栈进行开发。 核心功能模块 视频流展示…

vue 菜单实现

vue 菜单实现

Vue 菜单实现方法 在Vue中实现菜单功能可以通过多种方式完成,以下是几种常见的实现方法: 使用v-for动态生成菜单 通过数据驱动的方式动态渲染菜单项,适合菜单内容可能变化的场景: <t…

vue 实现图片

vue 实现图片

Vue 实现图片加载与展示的方法 在 Vue 中实现图片加载和展示可以通过多种方式完成,以下是一些常见的方法: 使用 v-bind 绑定图片路径 通过 v-bind(或简写为 :)动态绑定图片的…

黑马vue实现

黑马vue实现

以下是关于使用 Vue.js 实现黑马相关功能或项目的关键方法和实践指南: 环境配置与项目初始化 安装最新版 Vue CLI 或使用 Vite 创建项目: npm create vue@lates…