实现vue table
Vue 表格实现方法
基础表格实现
使用 Vue 的 v-for 指令可以快速渲染表格数据。在模板中定义表格结构,通过数据绑定动态生成行和列。
<template>
<table>
<thead>
<tr>
<th v-for="header in headers" :key="header">{{ header }}</th>
</tr>
</thead>
<tbody>
<tr v-for="item in items" :key="item.id">
<td v-for="(value, key) in item" :key="key">{{ value }}</td>
</tr>
</tbody>
</table>
</template>
<script>
export default {
data() {
return {
headers: ['ID', 'Name', 'Age'],
items: [
{ id: 1, name: 'Alice', age: 25 },
{ id: 2, name: 'Bob', age: 30 }
]
}
}
}
</script>
使用组件库
对于更复杂的需求,可以使用现成的 Vue 表格组件库如 Element UI、Vuetify 或 Ant Design Vue。
以 Element UI 为例:

<template>
<el-table :data="tableData">
<el-table-column prop="date" label="Date" width="180"></el-table-column>
<el-table-column prop="name" label="Name" width="180"></el-table-column>
<el-table-column prop="address" label="Address"></el-table-column>
</el-table>
</template>
<script>
export default {
data() {
return {
tableData: [{
date: '2023-01-01',
name: 'John',
address: 'New York'
}]
}
}
}
</script>
添加排序功能
通过计算属性或方法实现表格数据的排序功能。
<template>
<table>
<thead>
<tr>
<th @click="sortBy('name')">Name</th>
<th @click="sortBy('age')">Age</th>
</tr>
</thead>
<tbody>
<tr v-for="item in sortedItems" :key="item.id">
<td>{{ item.name }}</td>
<td>{{ item.age }}</td>
</tr>
</tbody>
</table>
</template>
<script>
export default {
data() {
return {
sortKey: '',
sortOrder: 1,
items: [
{ id: 1, name: 'Alice', age: 25 },
{ id: 2, name: 'Bob', age: 30 }
]
}
},
computed: {
sortedItems() {
return [...this.items].sort((a, b) => {
return (a[this.sortKey] > b[this.sortKey] ? 1 : -1) * this.sortOrder
})
}
},
methods: {
sortBy(key) {
this.sortOrder = this.sortKey === key ? -this.sortOrder : 1
this.sortKey = key
}
}
}
</script>
分页功能实现
结合计算属性实现前端分页功能。

<template>
<div>
<table>
<!-- 表格内容 -->
</table>
<div class="pagination">
<button @click="prevPage" :disabled="currentPage === 1">Previous</button>
<span>Page {{ currentPage }} of {{ totalPages }}</span>
<button @click="nextPage" :disabled="currentPage === totalPages">Next</button>
</div>
</div>
</template>
<script>
export default {
data() {
return {
currentPage: 1,
pageSize: 5,
items: [
// 数据数组
]
}
},
computed: {
paginatedItems() {
const start = (this.currentPage - 1) * this.pageSize
return this.items.slice(start, start + this.pageSize)
},
totalPages() {
return Math.ceil(this.items.length / this.pageSize)
}
},
methods: {
nextPage() {
if (this.currentPage < this.totalPages) this.currentPage++
},
prevPage() {
if (this.currentPage > 1) this.currentPage--
}
}
}
</script>
响应式设计
使用 CSS 或 UI 框架的响应式功能确保表格在不同设备上显示良好。
@media (max-width: 600px) {
table {
display: block;
overflow-x: auto;
white-space: nowrap;
}
}
性能优化
对于大型数据集,考虑使用虚拟滚动技术避免渲染所有行。
<template>
<div class="virtual-table" @scroll="handleScroll">
<div class="table-header">
<!-- 表头 -->
</div>
<div class="table-body" :style="{ height: totalHeight + 'px' }">
<div
v-for="item in visibleItems"
:key="item.id"
class="table-row"
:style="{ transform: `translateY(${item.offset}px)` }"
>
<!-- 行内容 -->
</div>
</div>
</div>
</template>






