基于js實(shí)現(xiàn)數(shù)組相鄰元素上移下移
實(shí)現(xiàn)效果:
即需要實(shí)現(xiàn)當(dāng)前元素與相鄰元素交換位置,
當(dāng)上移時(shí),則是當(dāng)前元素與上一元素調(diào)換位置;當(dāng)下移時(shí),則是當(dāng)前元素與下一元素調(diào)換位置。
實(shí)現(xiàn)代碼:
js:
//點(diǎn)擊上移clickUp(index){ this.swapArray(this.tableData, index-1, index);},//點(diǎn)擊下移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.cgvv.com.cn/bcjs/@/assets/images/up_01.png' v-if='scope.$index == 0'> <img src='http://www.cgvv.com.cn/bcjs/@/assets/images/up.png' @click='clickUp(scope.$index)' v-else> <img src='http://www.cgvv.com.cn/bcjs/@/assets/images/down_01.png' v-if='scope.$index == tableData.length - 1'> <img src='http://www.cgvv.com.cn/bcjs/@/assets/images/down.png' @click='clickDown(scope.$index)' v-else> </div> </template></el-table-column>
注意:
1.思想就是在數(shù)組中交換兩個(gè)元素的位置,使用splice()的替換;
2.上移是跟上一元素交換位置,下移是跟下一元素交換位置,不同體現(xiàn)在調(diào)用調(diào)換方法時(shí)傳入的index參數(shù)不同。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. chat.asp聊天程序的編寫方法2. jsp實(shí)現(xiàn)textarea中的文字保存換行空格存到數(shù)據(jù)庫(kù)的方法3. XML入門的常見(jiàn)問(wèn)題(二)4. Jsp中request的3個(gè)基礎(chǔ)實(shí)踐5. JavaScrip簡(jiǎn)單數(shù)據(jù)類型隱式轉(zhuǎn)換的實(shí)現(xiàn)6. 得到XML文檔大小的方法7. ASP.NET Core實(shí)現(xiàn)中間件的幾種方式8. JSP+Servlet實(shí)現(xiàn)文件上傳到服務(wù)器功能9. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)10. 如何在jsp界面中插入圖片
