前言
ElementUI是餓了么前端開源的一個(gè)基于Vue的前端框架,已經(jīng)幫我們封裝好了一系列功能性的組件,比如柵格系統(tǒng)、表格、表單、樹形菜單、通知等。對(duì)于搞后臺(tái)管理界面的項(xiàng)目,特別是不需要考慮兼容ie8、ie9以下的項(xiàng)目、ElementUI是一個(gè)不錯(cuò)的選擇。
而且ElementUI的文檔寫得十分詳盡,參照demo可以很快上手。
本文主要介紹了關(guān)于vue + element實(shí)現(xiàn)表格分頁(yè)和前端搜索的相關(guān)內(nèi)容,分享出來(lái)供大家參考學(xué)習(xí),下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧。
實(shí)現(xiàn)思路
1.前端后臺(tái)管理會(huì)存在很多表格,表格數(shù)據(jù)過(guò)多就需要分頁(yè);
2.前端交互每次搜索如果都請(qǐng)求服務(wù)器會(huì)加大服務(wù)器的壓力,所以在數(shù)據(jù)量不是很大的情況下可以一次性將數(shù)據(jù)返回,前端做檢索
3.下面貼上一個(gè)demo
示例代碼
<template> <div> <el-input v-model="tableDataName" placeholder="請(qǐng)輸入姓名" ></el-input> <el-button type="primary" @click="doFilter">搜索</el-button> <el-button type="primary" @click="openData">展示數(shù)據(jù)</el-button> <el-table :data="tableDataEnd" border > <el-table-column prop="date" label="日期" width="180"> </el-table-column> <el-table-column prop="name" label="姓名" width="180"> </el-table-column> <el-table-column prop="address" label="地址"> </el-table-column> </el-table> <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[1, 2, 3, 4]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="totalItems"> </el-pagination> </div> </template> <script> export default { data() { return { tableDataBegin: [ { date: "2016-05-01", name: "王小虎", address: "上海市普陀區(qū)金沙江路 1518 弄" }, { date: "2016-05-02", name: "王小虎", address: "上海市普陀區(qū)金沙江路 1517 弄" }, { date: "2016-05-03", name: "王二虎", address: "上海市普陀區(qū)金沙江路 1519 弄" }, { date: "2016-05-04", name: "王二虎", address: "上海市普陀區(qū)金沙江路 1516 弄" }, { date: "2016-05-05", name: "王三虎", address: "上海市普陀區(qū)金沙江路 1518 弄" }, { date: "2016-05-06", name: "王三虎", address: "上海市普陀區(qū)金沙江路 1517 弄" }, { date: "2016-05-07", name: "王小虎", address: "上海市普陀區(qū)金沙江路 1519 弄" }, { date: "2016-05-08", name: "王小虎", address: "上海市普陀區(qū)金沙江路 1516 弄" } ], tableDataName: "", tableDataEnd: [], currentPage: 4, pageSize: 2, totalItems: 0, filterTableDataEnd:[], flag:false }; }, created() { this.totalItems = this.tableDataBegin.length; if (this.totalItems > this.pageSize) { for (let index = 0; index < this.pageSize; index++) { this.tableDataEnd.push(this.tableDataBegin[index]); } } else { this.tableDataEnd = this.tableDataBegin; } }, methods: { //前端搜索功能需要區(qū)分是否檢索,因?yàn)閷?duì)應(yīng)的字段的索引不同 //用兩個(gè)變量接收currentChangePage函數(shù)的參數(shù) doFilter() { if (this.tableDataName == "") { this.$message.warning("查詢條件不能為空!"); return; } this.tableDataEnd = [] //每次手動(dòng)將數(shù)據(jù)置空,因?yàn)闀?huì)出現(xiàn)多次點(diǎn)擊搜索情況 this.filterTableDataEnd=[] this.tableDataBegin.forEach((value, index) => { if(value.name){ if(value.name.indexOf(this.tableDataName)>=0){ this.filterTableDataEnd.push(value) } } }); //頁(yè)面數(shù)據(jù)改變重新統(tǒng)計(jì)數(shù)據(jù)數(shù)量和當(dāng)前頁(yè) this.currentPage=1 this.totalItems=this.filterTableDataEnd.length //渲染表格,根據(jù)值 this.currentChangePage(this.filterTableDataEnd) //頁(yè)面初始化數(shù)據(jù)需要判斷是否檢索過(guò) this.flag=true }, openData() {}, handleSizeChange(val) { console.log(`每頁(yè) ${val} 條`); this.pageSize = val; this.handleCurrentChange(this.currentPage); }, handleCurrentChange(val) { console.log(`當(dāng)前頁(yè): ${val}`); this.currentPage = val; //需要判斷是否檢索 if(!this.flag){ this.currentChangePage(this.tableDataEnd) }else{ this.currentChangePage(this.filterTableDataEnd) } }, //組件自帶監(jiān)控當(dāng)前頁(yè)碼 currentChangePage(list) { let from = (this.currentPage - 1) * this.pageSize; let to = this.currentPage * this.pageSize; this.tableDataEnd = []; for (; from < to; from++) { if (list[from]) { this.tableDataEnd.push(list[from]); } } } } }; </script>
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
分享文章:利用vue+element實(shí)現(xiàn)表格分頁(yè)和前端搜索的方法-創(chuàng)新互聯(lián)
本文地址:http://aaarwkj.com/article26/gipcg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、網(wǎng)站內(nèi)鏈、定制網(wǎng)站、小程序開發(fā)、動(dòng)態(tài)網(wǎng)站、商城網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容