Vue3中Element-Plus分頁(Pagination)組件的使用
開發(fā)過程中數(shù)據(jù)展示會經(jīng)常使用到,同時分頁功能也會添加到頁面中。
記:在Vue3中使用Element-Plus分頁組件與表格數(shù)據(jù)實現(xiàn)分頁交互。
開始實現(xiàn)
引入表格和分頁組件的H5標簽。
<strong>Element-Plus分頁組件使用</strong> <div> <el-table :data="tableData" style="width: 100%"> <el-table-column prop="id" label="這里是id" width="180" /> <el-table-column prop="data" label="這里是一些數(shù)據(jù)" width="180" /> </el-table> <el-pagination :current-page="searchData.current" :page-size="searchData.limit" :total="total" :pager-count="6" style="text-align: center;margin-top: 20px;" layout="jumper, prev, pager, next, total" @current-change="getData" /> </div>
js代碼,先初始化變量。
<script setup> import {ref,reactive,onMounted} from 'vue' // tableData-表格數(shù)據(jù)列表,total-數(shù)據(jù)總長度 const tableData=ref([]) const total=ref(0) // searchData-向后端分頁查詢的對象,即當前頁和每頁總數(shù) const searchData=reactive({ current:1, limit:10 }) ... </script>
沒用到后臺,所以就把表格的數(shù)據(jù)寫固定了。下面就表格數(shù)據(jù)生成,還有模擬對數(shù)據(jù)的分頁。
//表格數(shù)據(jù)生成 function tableAddData(){ //給表格添加數(shù)據(jù),調(diào)接口賦值同理 var index=0 //因為數(shù)據(jù)是固定生成的,容易出錯,所以這里要清一下 tableData.value=[] for(var i=1;i<=101;i++){ let data={} data.id=i data.data=`我的數(shù)據(jù)是:${i}` tableData.value.push(data) index+=1 } total.value=index } //傳入分頁參數(shù) function pageQuery(current,limit){ // 模仿分頁查詢,將表格的數(shù)據(jù)裁切一下 // 1 2 3 //下標 0-9 10-19 20-29 let begin=current*limit-limit //這里不減一是因為,slice方法裁切是左閉右開數(shù)組 let end=current*limit tableData.value=tableData.value.slice(begin,end) }
方法調(diào)用,這里需要注意幾個地方。
1. 第一次加載getData方法時,方法內(nèi)的默認傳的參數(shù)是空的,所以就賦個1,不然不太友好。
2. 分頁組件的@current-change調(diào)用的方法默認會傳入一個參數(shù),即點擊的頁碼數(shù)。所以實現(xiàn)點擊跳轉,就要把分頁查詢參數(shù)的當前頁current賦該值。
function getData(val = 1){ searchData.current=val // 先把數(shù)據(jù)搞上 tableAddData() pageQuery(searchData.current,searchData.limit) } onMounted(async()=>{ getData() })
到這里就可以測試查看一下了
初次加載
點擊頁碼,頁面跳轉
測試這里的輸入跳轉功能也沒問題,總數(shù)據(jù)也正常
到此這篇關于Vue3中Element-Plus分頁(Pagination)組件的使用的文章就介紹到這了,更多相關Vue3 Element-Plus分頁組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
vue使用自定義事件的表單輸入組件用法詳解【日期組件與貨幣組件】
這篇文章主要介紹了vue使用自定義事件的表單輸入組件用法,結合實例形式詳細分析了vue.js日期組件與貨幣組件相關操作技巧及注意事項,需要的朋友可以參考下2020-06-06vue elementUI el-form 數(shù)據(jù)無法賦值且不報錯的問題及解決方法
vue項目中使用elementUI的el-form組件,里面有部分后端數(shù)據(jù)遍歷的字段和部分確定的字段,遇到個問題遍歷的字段可以修改值但是確定的幾個字段無法修改值,下面小編給大家分享vue elementUI el-form 數(shù)據(jù)無法賦值且不報錯的問題及解決方法,感興趣的朋友一起看看吧2023-12-12vue解決使用$http獲取數(shù)據(jù)時報錯的問題
今天小編就為大家分享一篇vue解決使用$http獲取數(shù)據(jù)時報錯的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10