如果不從后臺(tái)請(qǐng)求數(shù)據(jù),那么就需要在前端手動(dòng)管理數(shù)據(jù)??梢允褂靡韵虏襟E實(shí)現(xiàn)該功能:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-511455.html
- 在 Vue 組件的 data 中定義一個(gè)數(shù)組來(lái)存放所有數(shù)據(jù)(不分頁(yè))。
data() {
return {
allData: [], // 所有數(shù)據(jù)
currentPage: 1, // 當(dāng)前頁(yè)數(shù)
pageSize: 10, // 每頁(yè)顯示的條數(shù)
}
}
- 在 mounted 鉤子函數(shù)中,手動(dòng)獲取數(shù)據(jù)并存放到上一步定義的數(shù)組中。
mounted() {
// 手動(dòng)獲取數(shù)據(jù)
this.allData = [
{ name: '張三', age: 18, gender: '男' },
{ name: '李四', age: 22, gender: '女' },
{ name: '王五', age: 26, gender: '男' },
// ... 其他數(shù)據(jù)
]
}
- 在模板中使用 element-ui 的表格組件來(lái)展示數(shù)據(jù),同時(shí)使用 el-pagination 組件來(lái)實(shí)現(xiàn)分頁(yè)。其中,表格的數(shù)據(jù)通過(guò)計(jì)算屬性來(lái)獲取,分頁(yè)的總數(shù)需要根據(jù)數(shù)據(jù)總條數(shù)和每頁(yè)顯示的條數(shù)計(jì)算得出。
<template>
<div>
<el-table :data="tableData" style="width: 100%">
<el-table-column label="姓名" prop="name"></el-table-column>
<el-table-column label="年齡" prop="age"></el-table-column>
<el-table-column label="性別" prop="gender"></el-table-column>
</el-table>
<el-pagination
layout="total, sizes, prev, pager, next, jumper"
:total="total"
:page-size="pageSize"
v-model="currentPage"
></el-pagination>
</div>
</template>
<script>
export default {
data() {
return {
allData: [],
currentPage: 1,
pageSize: 10,
}
},
mounted() {
// 手動(dòng)獲取數(shù)據(jù)
this.allData = [
{ name: '張三', age: 18, gender: '男' },
{ name: '李四', age: 22, gender: '女' },
{ name: '王五', age: 26, gender: '男' },
// ... 其他數(shù)據(jù)
]
},
methods: {
// 獲取當(dāng)前頁(yè)的數(shù)據(jù)
getCurrentPageData() {
const start = (this.currentPage - 1) * this.pageSize
const end = start + this.pageSize
return this.allData.slice(start, end)
},
},
computed: {
// 計(jì)算分頁(yè)總數(shù)
total() {
return Math.ceil(this.allData.length / this.pageSize)
},
// 計(jì)算當(dāng)前頁(yè)的數(shù)據(jù)
tableData() {
return this.getCurrentPageData()
},
},
}
</script>
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-511455.html
到了這里,關(guān)于vue搭配element-ui前端實(shí)現(xiàn)表格分頁(yè)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!