基于前端VUE+ElementUI實(shí)現(xiàn)table行上移或下移功能(支持跨頁移動)
最近在做后臺管理遇見了一個這樣的需求:table列表需要支持上下移動數(shù)據(jù),并且也需要滿足跨頁移動,前端把數(shù)據(jù)移動整理之后,提交給后端進(jìn)行保存(平常這種數(shù)據(jù)移動都是調(diào)用后端的接口,然后在查詢數(shù)據(jù)就可以完成了,但是這次顯然不能這么做,因?yàn)楹蠖酥挥幸粋€保存數(shù)據(jù)的接口,所以這就需要前端自己處理數(shù)據(jù)了,廢話少說,上效果圖和源碼!
靜態(tài)效果圖
動態(tài)效果圖
實(shí)現(xiàn)源碼(HTML)
<el-table :data="paginatedData"> <el-table-column label="操作" prop="operate"> <template slot-scope="scope"> <el-button-group> <el-button title="下移" :disabled="isDown(scope.row)" @click="moveupOrmovedown(scope.row, scope.$index, 'down')" > </el-button> <el-button title="上移" :disabled="scope.$index === 0 && currentPage === 1" @click="moveupOrmovedown(scope.row, scope.$index, 'up')" > </el-button> </el-button-group> </template> </el-table-column> </el-table> <!-- 頁碼參考(此處不涉及該功能的任何邏輯,可忽略 --> <el-pagination background :page-size="pageSize" :current-page="currentPage" layout="total, prev, pager, next, jumper" :total="totalSize" @current-change="(val) => (currentPage = val)" > </el-pagination>
實(shí)現(xiàn)源碼(JS)
moveupOrmovedown(row, index, type) { let arr = this.filteredData const findIndex = this.filteredData.findIndex( (item) => item.date === row.date ) index = findIndex > this.pageSize - 1 ? findIndex : index type === 'up' ? arr.splice(index - 1, 1, ...arr.splice(index, 1, arr[index - 1])) : arr.splice(index, 1, ...arr.splice(index + 1, 1, arr[index])) }
詳情源碼(僅展示參數(shù))
<script> export default { data() { return { totalSize: 0, currentPage: 1, pageSize: 10, filteredData: [], paginatedData: [], tableData: [] } }, methods: { isDown(row) { const findIndex = this.filteredData.findIndex( (item) => item.date === row.date ) return findIndex === this.filteredData.length - 1 }, moveupOrmovedown(row, index, type) { let arr = this.filteredData const findIndex = this.filteredData.findIndex( (item) => item.date === row.date ) index = findIndex > this.pageSize - 1 ? findIndex : index type === 'up' ? arr.splice(index - 1, 1, ...arr.splice(index, 1, arr[index - 1])) : arr.splice(index, 1, ...arr.splice(index + 1, 1, arr[index])) }, handleCurrentChange(val) { this.currentPage = val }, selectCheckBox(selectCheckBox) { const newFilterData = this.filterDataByDate( this.tableData, selectCheckBox ) this.filteredData = [...this.filteredData, ...newFilterData] }, paginateData(data, pageSize, currentPage) { if (data.length < 11) return data const startIndex = (currentPage - 1) * pageSize const endIndex = startIndex + pageSize const dataToShow = data.slice(startIndex, endIndex) return dataToShow }, updatePaginatedData() { this.totalSize = this.filteredData.length // 分頁(前端處理) // this.paginatedData = this.$util.paginateData( this.paginatedData = this.paginateData( this.filteredData, this.pageSize, this.currentPage ) } }, created() { // 調(diào)后端接口返回的全部數(shù)據(jù)(后面前端自己分頁) this.tableData = tableData }, mounted() {}, watch: { currentPage: { handler(newPage) { this.updatePaginatedData() }, immediate: true, }, filteredData: { handler(newArray) { this.updatePaginatedData() }, immediate: true, } }, computed: {}, filters: {} } </script>
總結(jié)
到此這篇關(guān)于前端VUE+ElementUI實(shí)現(xiàn)table行上移或下移功能的文章就介紹到這了,更多相關(guān)VUE+ElementUI實(shí)現(xiàn)table行上移或下移內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue-cli實(shí)現(xiàn)多頁面多路由的示例代碼
本篇文章主要介紹了vue-cli實(shí)現(xiàn)多頁面多路由的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01Vue SSR 即時編譯技術(shù)的實(shí)現(xiàn)
這篇文章主要介紹了Vue SSR 即時編譯技術(shù)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05Vue如何獲取new Date().getTime()時間戳
在Web開發(fā)中,前端使用Vue.js獲取的是毫秒級時間戳,而PHP后端則是秒級時間戳,處理此類問題時,需要將PHP的時間戳乘以1000轉(zhuǎn)換為毫秒級,以保證數(shù)據(jù)的一致性和正確的邏輯判斷2024-10-10vue如何在用戶要關(guān)閉當(dāng)前網(wǎng)頁時彈出提示的實(shí)現(xiàn)
這篇文章主要介紹了vue如何在用戶要關(guān)閉當(dāng)前網(wǎng)頁時彈出提示的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05Vues中使用JavaScript實(shí)現(xiàn)路由跳轉(zhuǎn)的步驟詳解
在Vue應(yīng)用中,利用Vue?Router進(jìn)行頁面間的導(dǎo)航是一個常見需求,本篇博客將通過示例代碼詳細(xì)介紹如何在Vue組件中使用JavaScript實(shí)現(xiàn)路由跳轉(zhuǎn),需要的朋友可以參考下2024-05-05如何實(shí)現(xiàn)雙向綁定mvvm的原理實(shí)現(xiàn)
這篇文章主要介紹了vue雙向數(shù)據(jù)綁定原理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05Element?table?上下移需求的實(shí)現(xiàn)
本文主要介紹了Element?table?上下移需求的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07關(guān)于Vue項(xiàng)目使用scss終端發(fā)出警告解決方法
這篇文章主要介紹了關(guān)于Vue項(xiàng)目使用scss終端發(fā)出警告的解決方法,出現(xiàn)這個問題的原因是項(xiàng)目中使用了DartSass舊版的JavaScriptAPI,這些API已被棄用,文章將解決的辦法介紹的非常詳細(xì),需要的朋友可以參考下2025-04-04