基于js實現(xiàn)數(shù)組相鄰元素上移下移
實現(xiàn)效果:
即需要實現(xiàn)當(dāng)前元素與相鄰元素交換位置,
當(dāng)上移時,則是當(dāng)前元素與上一元素調(diào)換位置;當(dāng)下移時,則是當(dāng)前元素與下一元素調(diào)換位置。
實現(xiàn)代碼:
js:
//點擊上移clickUp(index){ this.swapArray(this.tableData, index-1, index);},//點擊下移clickDown(index){ this.swapArray(this.tableData, index, index+1);},//數(shù)組元素互換位置swapArray(arr, index1, index2) { arr[index1] = arr.splice(index2, 1, arr[index1])[0]; return arr;},
html:
<el-table-column label='順序調(diào)整' min- align='center'> <template slot-scope='scope'> <div class='img_style'> <img src='http://www.piao2010.com/bcjs/@/assets/images/up_01.png' v-if='scope.$index == 0'> <img src='http://www.piao2010.com/bcjs/@/assets/images/up.png' @click='clickUp(scope.$index)' v-else> <img src='http://www.piao2010.com/bcjs/@/assets/images/down_01.png' v-if='scope.$index == tableData.length - 1'> <img src='http://www.piao2010.com/bcjs/@/assets/images/down.png' @click='clickDown(scope.$index)' v-else> </div> </template></el-table-column>
注意:
1.思想就是在數(shù)組中交換兩個元素的位置,使用splice()的替換;
2.上移是跟上一元素交換位置,下移是跟下一元素交換位置,不同體現(xiàn)在調(diào)用調(diào)換方法時傳入的index參數(shù)不同。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 使用Spry輕松將XML數(shù)據(jù)顯示到HTML頁的方法2. 輕松學(xué)習(xí)XML教程3. ASP中解決“對象關(guān)閉時,不允許操作。”的詭異問題……4. ASP使用MySQL數(shù)據(jù)庫的方法5. msxml3.dll 錯誤 800c0019 系統(tǒng)錯誤:-2146697191解決方法6. ASP動態(tài)網(wǎng)頁制作技術(shù)經(jīng)驗分享7. 得到XML文檔大小的方法8. html小技巧之td,div標(biāo)簽里內(nèi)容不換行9. WMLScript的語法基礎(chǔ)10. ASP中if語句、select 、while循環(huán)的使用方法
