当前位置:首页 > 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 Router实现。Vue Router是Vue.js官方的路由管理器,用于构建单页面应用(SPA)。 基本路由配置 安装Vue Router后…

vue实现收起展开面板

vue实现收起展开面板

实现收起展开面板的方法 在Vue中实现收起展开面板功能可以通过多种方式完成,以下是几种常见的实现方法: 使用v-show或v-if控制显示隐藏 利用Vue的指令可以轻松实现面板的展开和收起: &l…

vue实现ssr

vue实现ssr

Vue 实现 SSR(服务端渲染) Vue 的服务端渲染(SSR)允许在服务器端生成完整的 HTML 内容,再发送到客户端,提升首屏加载速度和 SEO 友好性。以下是实现 Vue SSR 的关键步骤和…

vue单选实现

vue单选实现

Vue 单选实现 在 Vue 中实现单选功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-model 绑定单选按钮 通过 v-model 绑定一个变量,可以实现单选效果。当用户选择不同的选…

vue实现注册

vue实现注册

Vue 实现注册功能 注册功能通常涉及前端表单、数据验证、与后端 API 的交互等步骤。以下是一个完整的 Vue 实现注册功能的方案。 创建注册表单 使用 Vue 的模板语法创建注册表单,包含用户名…

vue watch实现

vue watch实现

在 Vue 中实现 watch 监听数据变化 Vue 的 watch 选项用于监听数据变化并执行相应的回调函数。以下是几种常见的实现方式: 基础用法 export default { data(…