vue表格实现教学
Vue 表格实现方法
使用原生HTML表格
在Vue模板中直接使用HTML的<table>标签,结合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>
使用第三方表格组件库
Element UI的表格组件实现:
<template>
<el-table :data="tableData" style="width: 100%">
<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: 'Alice', address: 'New York' },
{ date: '2023-01-02', name: 'Bob', address: 'London' }
]
}
}
}
</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() {
if (!this.sortKey) return this.items
return [...this.items].sort((a, b) => {
return (a[this.sortKey] > b[this.sortKey] ? 1 : -1) * this.sortOrder
})
}
},
methods: {
sortBy(key) {
if (this.sortKey === key) {
this.sortOrder *= -1
} else {
this.sortKey = key
this.sortOrder = 1
}
}
}
}
</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 {
items: [...], // 所有数据
currentPage: 1,
itemsPerPage: 10
}
},
computed: {
paginatedItems() {
const start = (this.currentPage - 1) * this.itemsPerPage
return this.items.slice(start, start + this.itemsPerPage)
},
totalPages() {
return Math.ceil(this.items.length / this.itemsPerPage)
}
},
methods: {
nextPage() {
if (this.currentPage < this.totalPages) this.currentPage++
},
prevPage() {
if (this.currentPage > 1) this.currentPage--
}
}
}
</script>
表格性能优化
对于大数据量表格,使用虚拟滚动技术:
<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>
<script>
export default {
data() {
return {
items: [...], // 所有数据
rowHeight: 50,
visibleCount: 20,
startIndex: 0
}
},
computed: {
totalHeight() {
return this.items.length * this.rowHeight
},
visibleItems() {
return this.items
.slice(this.startIndex, this.startIndex + this.visibleCount)
.map((item, i) => ({
...item,
offset: (this.startIndex + i) * this.rowHeight
}))
}
},
methods: {
handleScroll(e) {
const scrollTop = e.target.scrollTop
this.startIndex = Math.floor(scrollTop / this.rowHeight)
}
}
}
</script>






