Vue如何實(shí)現(xiàn)數(shù)據(jù)的上移和下移
更新時(shí)間:2024年06月07日 17:05:38 作者:小林犟
這篇文章主要介紹了Vue如何實(shí)現(xiàn)數(shù)據(jù)的上移和下移問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
Vue實(shí)現(xiàn)數(shù)據(jù)的上移和下移
場(chǎng)景
點(diǎn)擊上移
或下移
按鈕進(jìn)行列表移動(dòng),第一行
則不能上移
,最后一行
則不能下移
解決方案
<el-button @click="moveUp(index)">上移</el-button> <el-button @click="moveDown(index)">下移</el-button> data() { return { list: [ { id: 1, name: '張三' }, { id: 2, name: '李四' }, { id: 3, name: '王五' } ] } } // 上移 moveUp (index) { const arr = this.list arr.splice(index - 1, 1, ...arr.splice(index, 1, arr[index - 1])) }, // 下移 moveDown (index) { const arr = this.list arr.splice(index, 1, ...arr.splice(index + 1, 1, arr[index])) },
禁用上下移邏輯
- 禁用上移:
:disabled="index === 0"
- 禁用下移:
:disabled="index === list.length - 1"
Vue表單批量上移 下移
效果圖
// 上移 handDmoveUp () { //選中行數(shù)據(jù) let arrChecked = this.$refs.ref_ri_table.getCheckboxRecords(); //表格數(shù)據(jù) let arr = this.tableData; //正序遍歷,保證移動(dòng)完成的數(shù)據(jù)在下一次循環(huán)時(shí)位置不會(huì)再變動(dòng) a: for (let index1 = 0; index1 < arrChecked.length; index1++) { b: for (let index2 = 0; index2 < arr.length; index2++) { //選中數(shù)據(jù)定位到其在總數(shù)據(jù)中的位置時(shí)開始上移 if (arrChecked[index1] === arr[index2]) { //選中數(shù)據(jù)與總數(shù)據(jù)索引相同時(shí),說明已經(jīng)上移到最上層,結(jié)束這層 //循環(huán) if (index1 === index2) { break b; } //上移一位到達(dá)上一條數(shù)據(jù)的上方 arr.splice(index2 - 1, 0, arr[index2]); //刪除原數(shù)據(jù) arr.splice(index2 + 1, 1); //上移完成結(jié)束內(nèi)存循環(huán),開始移動(dòng)下一條選中數(shù)據(jù) break b; } } } }, //下移 handMoveDown () { let arrChecked = this.$refs.ref_ri_table.getCheckboxRecords(); let arr = this.tableData; a: for (let index1 = arrChecked.length - 1; index1 >= 0; index1--) { b: for (let index2 = arr.length - 1; index2 >= 0; index2--) { if (arrChecked[index1] === arr[index2]) { //選中數(shù)據(jù)索引+表格數(shù)組長(zhǎng)度-選中數(shù)組長(zhǎng)度=選中數(shù)據(jù)索引,代表以及下移到最底部,結(jié)束下移 if (index1 + arr.length - arrChecked.length === index2) { break b; } arr.splice(index2 + 2, 0, arr[index2]); arr.splice(index2, 1); break b; } } } },
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue 3自定義指令開發(fā)的相關(guān)總結(jié)
這篇文章主要介紹了Vue 3自定義指令開發(fā)的相關(guān)總結(jié),幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下2021-01-01vue 行為驗(yàn)證碼之滑動(dòng)驗(yàn)證AJ-Captcha使用詳解
這篇文章主要介紹了vue 行為驗(yàn)證碼之滑動(dòng)驗(yàn)證AJ-Captcha使用詳解,AJ-Captcha不需要npm安裝,只需要將組件 verifition復(fù)制到所使用的components目錄下,本文給大家詳細(xì)講解,需要的朋友可以參考下2023-05-05vue 中常用操作數(shù)組的方法(forEach()和reduce())
文章介紹了JavaScript中常用的操作數(shù)組方法,包括forEach()和reduce(),forEach()用于遍歷數(shù)組并對(duì)每個(gè)元素執(zhí)行操作,而reduce()則用于遍歷數(shù)組并進(jìn)行累加等迭代操作,感興趣的朋友一起看看吧2025-03-03vue-mugen-scroll組件實(shí)現(xiàn)pc端滾動(dòng)刷新
這篇文章主要為大家詳細(xì)介紹了vue-mugen-scroll組件實(shí)現(xiàn)pc端滾動(dòng)刷新,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08vue3頁面跳轉(zhuǎn)傳值時(shí)獲取不到params值的問題解決
在Vue3頁面跳轉(zhuǎn)時(shí)傳遞和獲取參數(shù)通常通過路由參數(shù)和查詢字符串實(shí)現(xiàn),本文主要介紹了vue3頁面跳轉(zhuǎn)傳值時(shí)獲取不到params值的問題解決,感興趣的可以了解一下2024-11-11