用vue实现表格
使用 Vue 实现表格
Vue 提供了灵活的方式来实现动态表格,可以通过 v-for 指令结合数组数据渲染表格内容。以下是一个完整的实现示例。
基础表格实现
创建一个 Vue 组件,定义表格的数据结构和模板:

<template>
<div>
<table class="table">
<thead>
<tr>
<th v-for="(header, index) in headers" :key="index">
{{ header }}
</th>
</tr>
</thead>
<tbody>
<tr v-for="(row, rowIndex) in rows" :key="rowIndex">
<td v-for="(cell, cellIndex) in row" :key="cellIndex">
{{ cell }}
</td>
</tr>
</tbody>
</table>
</div>
</template>
<script>
export default {
data() {
return {
headers: ["ID", "Name", "Age", "Email"],
rows: [
[1, "John Doe", 25, "john@example.com"],
[2, "Jane Smith", 30, "jane@example.com"],
[3, "Bob Johnson", 22, "bob@example.com"],
],
};
},
};
</script>
<style>
.table {
width: 100%;
border-collapse: collapse;
}
.table th, .table td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
.table th {
background-color: #f2f2f2;
}
</style>
动态数据绑定
如果需要从 API 获取数据,可以使用 axios 或 fetch 加载数据:

<template>
<div>
<table class="table">
<!-- 表头同上 -->
<tbody>
<tr v-for="item in items" :key="item.id">
<td>{{ item.id }}</td>
<td>{{ item.name }}</td>
<td>{{ item.age }}</td>
<td>{{ item.email }}</td>
</tr>
</tbody>
</table>
</div>
</template>
<script>
export default {
data() {
return {
items: [],
};
},
async created() {
try {
const response = await fetch("https://api.example.com/users");
this.items = await response.json();
} catch (error) {
console.error("Error fetching data:", error);
}
},
};
</script>
添加排序功能
通过计算属性实现表格数据的排序:
<template>
<div>
<table class="table">
<thead>
<tr>
<th @click="sortBy('id')">ID</th>
<th @click="sortBy('name')">Name</th>
<th @click="sortBy('age')">Age</th>
<th @click="sortBy('email')">Email</th>
</tr>
</thead>
<tbody>
<tr v-for="item in sortedItems" :key="item.id">
<td>{{ item.id }}</td>
<td>{{ item.name }}</td>
<td>{{ item.age }}</td>
<td>{{ item.email }}</td>
</tr>
</tbody>
</table>
</div>
</template>
<script>
export default {
data() {
return {
items: [
{ id: 1, name: "John Doe", age: 25, email: "john@example.com" },
{ id: 2, name: "Jane Smith", age: 30, email: "jane@example.com" },
{ id: 3, name: "Bob Johnson", age: 22, email: "bob@example.com" },
],
sortKey: "",
sortOrder: 1, // 1 for ascending, -1 for descending
};
},
computed: {
sortedItems() {
if (!this.sortKey) return this.items;
return [...this.items].sort((a, b) => {
const valA = a[this.sortKey];
const valB = b[this.sortKey];
return this.sortOrder * (valA < valB ? -1 : valA > valB ? 1 : 0);
});
},
},
methods: {
sortBy(key) {
if (this.sortKey === key) {
this.sortOrder *= -1;
} else {
this.sortKey = key;
this.sortOrder = 1;
}
},
},
};
</script>
添加分页功能
通过计算属性实现分页:
<template>
<div>
<table class="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: 5,
};
},
computed: {
paginatedItems() {
const start = (this.currentPage - 1) * this.itemsPerPage;
const end = start + this.itemsPerPage;
return this.items.slice(start, end);
},
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>
以上示例展示了 Vue 中实现表格的常见功能,包括基础渲染、动态数据加载、排序和分页。根据实际需求可以进一步扩展功能。






