当前位置:首页 > 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提升性能:

<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增强可读性:

.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)

分页功能实现

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

vue实现学生列表

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>

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

相关文章

uniapp商品列表

uniapp商品列表

商品列表实现方法 在uniapp中实现商品列表功能,可以通过多种方式完成,以下是几种常见的方法: 使用scroll-view组件实现滚动列表 <scroll-view scroll-y="t…

uniapp 内容列表

uniapp 内容列表

uniapp 内容列表的实现方法 在uniapp中实现内容列表通常需要结合组件和数据绑定。以下是一个常见的方法: 模板部分 <template> <view class="c…

vue实现列表页面

vue实现列表页面

Vue 列表页面实现方案 基础列表渲染 使用 v-for 指令渲染数组数据,配合 :key 提升性能: <template> <ul> <li v-for=…

vue实现列表选择

vue实现列表选择

Vue 实现列表选择的方法 使用 v-model 绑定数组 通过 v-model 绑定一个数组,可以轻松实现多选功能。适用于复选框场景。 <template> <div>…

vue实现视频列表

vue实现视频列表

Vue 实现视频列表 在 Vue 中实现视频列表通常需要结合数据绑定、组件化和第三方库(如视频播放器)。以下是具体实现方法: 数据准备 创建一个数组存储视频信息,包括标题、封面图、视频链接等: d…

vue实现列表轮播

vue实现列表轮播

实现列表轮播的基本思路 使用Vue实现列表轮播的核心在于动态更新列表数据,结合CSS动画或过渡效果实现平滑切换。通常有两种实现方式:基于CSS动画的无限循环轮播和基于Vue数据绑定的动态更新轮播。…