基于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.gepszalag.com/bcjs/@/assets/images/up_01.png' v-if='scope.$index == 0'> <img src='http://www.gepszalag.com/bcjs/@/assets/images/up.png' @click='clickUp(scope.$index)' v-else> <img src='http://www.gepszalag.com/bcjs/@/assets/images/down_01.png' v-if='scope.$index == tableData.length - 1'> <img src='http://www.gepszalag.com/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. ASP.NET MVC使用jQuery ui的progressbar實(shí)現(xiàn)進(jìn)度條2. Ajax對(duì)xml信息的接收和處理操作實(shí)例分析3. Jsp中request的3個(gè)基礎(chǔ)實(shí)踐4. Ajax返回值類(lèi)型與用法實(shí)例分析5. python簡(jiǎn)單實(shí)現(xiàn)9宮格圖片實(shí)例6. 利用Java對(duì)PDF文件進(jìn)行電子簽章的實(shí)戰(zhàn)過(guò)程7. php根據(jù)id生成10位不重復(fù)數(shù)字跟字母混合字符串8. ASP動(dòng)態(tài)include文件9. Java實(shí)戰(zhàn)之實(shí)現(xiàn)一個(gè)好用的MybatisPlus代碼生成器10. Python request中文亂碼問(wèn)題解決方案
