当前位置:首页 > VUE

vue实现学生列表

2026-01-21 04:00:10VUE

实现学生列表的Vue方案

数据准备

在Vue组件中定义学生数据数组,通常放在datasetup中。示例使用Composition API:

const students = ref([
  { id: 1, name: '张三', age: 18, grade: '高一' },
  { id: 2, name: '李四', age: 17, grade: '高二' }
])

列表渲染

使用v-for指令渲染学生列表,建议添加:key提升性能:

vue实现学生列表

<ul>
  <li v-for="student in students" :key="student.id">
    {{ student.name }} - 年龄: {{ student.age }} 年级: {{ student.grade }}
  </li>
</ul>

表格形式展示

更规范的展示方式可采用表格布局:

<table class="student-table">
  <thead>
    <tr>
      <th>ID</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.grade }}</td>
    </tr>
  </tbody>
</table>

样式优化

添加基础CSS增强可读性:

vue实现学生列表

.student-table {
  width: 100%;
  border-collapse: collapse;
}
.student-table th, .student-table td {
  border: 1px solid #ddd;
  padding: 8px;
}
.student-table tr:nth-child(even) {
  background-color: #f2f2f2;
}

动态数据加载

实际项目通常从API获取数据:

const fetchStudents = async () => {
  try {
    const response = await axios.get('/api/students')
    students.value = response.data
  } catch (error) {
    console.error('获取学生列表失败:', error)
  }
}
onMounted(fetchStudents)

分页功能实现

处理大数据量的分页展示:

const pagination = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 0
})

const paginatedStudents = computed(() => {
  const start = (pagination.currentPage - 1) * pagination.pageSize
  return students.value.slice(start, start + pagination.pageSize)
})

完整组件示例

<template>
  <div>
    <table class="student-table">
      <!-- 表格内容同上 -->
    </table>
    <div class="pagination">
      <button @click="pagination.currentPage--" :disabled="pagination.currentPage === 1">上一页</button>
      <span>第{{ pagination.currentPage }}页</span>
      <button @click="pagination.currentPage++" :disabled="pagination.currentPage * pagination.pageSize >= pagination.total">下一页</button>
    </div>
  </div>
</template>

<script setup>
// 脚本内容同上
</script>

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

相关文章

vue实现搜索列表

vue实现搜索列表

Vue 实现搜索列表 在 Vue 中实现搜索列表功能,通常需要结合数据绑定、计算属性和事件监听。以下是一个完整的实现方案: 数据准备与模板结构 <template> <div…

vue实现列表

vue实现列表

实现列表的基本方法 在Vue中实现列表通常使用v-for指令,这是Vue的核心功能之一。v-for可以遍历数组或对象,为每个元素生成对应的DOM节点。 <template> <…

vue实现列表权限

vue实现列表权限

实现列表权限控制的方法 在Vue中实现列表权限控制通常涉及前端逻辑与后端数据的配合。以下是几种常见方法: 基于角色或权限码的渲染控制 通过v-if或v-show指令根据用户权限动态渲染列表项:…

vue实现文本列表

vue实现文本列表

Vue 实现文本列表的方法 Vue 可以通过多种方式实现文本列表的渲染和管理,以下是几种常见的方法: 使用 v-for 指令渲染列表 通过 v-for 指令可以动态渲染数组或对象中的文本列表。…

vue实现列表全选

vue实现列表全选

实现全选功能的基本思路 在Vue中实现列表全选功能,通常需要结合v-model和计算属性来管理选中状态。全选按钮的状态与列表中所有项的选中状态相互关联。 基础实现方法 准备一个数据列表和对应的选中…

vue实现虚拟列表

vue实现虚拟列表

虚拟列表的概念 虚拟列表(Virtual List)是一种优化长列表渲染性能的技术,通过仅渲染可视区域内的元素,减少DOM节点数量,从而提升页面性能。适用于数据量大的场景(如表格、下拉选择器等)。…