基于js实现数组相邻元素上移下移
实现效果:

即需要实现当前元素与相邻元素交换位置,
当上移时,则是当前元素与上一元素调换位置;当下移时,则是当前元素与下一元素调换位置。
实现代码:
js:
//点击上移clickUp(index){ this.swapArray(this.tableData, index-1, index);},//点击下移clickDown(index){ this.swapArray(this.tableData, index, index+1);},//数组元素互换位置swapArray(arr, index1, index2) { arr[index1] = arr.splice(index2, 1, arr[index1])[0]; return arr;},
html:
<el-table-column label='顺序调整' min- align='center'> <template slot-scope='scope'> <div class='img_style'> <img src='https://www.haobala.com/bcjs/@/assets/images/up_01.png' v-if='scope.$index == 0'> <img src='https://www.haobala.com/bcjs/@/assets/images/up.png' @click='clickUp(scope.$index)' v-else> <img src='https://www.haobala.com/bcjs/@/assets/images/down_01.png' v-if='scope.$index == tableData.length - 1'> <img src='https://www.haobala.com/bcjs/@/assets/images/down.png' @click='clickDown(scope.$index)' v-else> </div> </template></el-table-column>
注意:
1.思想就是在数组中交换两个元素的位置,使用splice()的替换;
2.上移是跟上一元素交换位置,下移是跟下一元素交换位置,不同体现在调用调换方法时传入的index参数不同。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持好吧啦网。
相关文章:
1. H5页面使用audio标签播放音频2. Ajax 的初步实现(使用vscode+node.js+express框架)3. jsp request.getParameter() 和request.getAttribute()方法区别详解4. 利用CSS3新特性创建透明边框三角5. python 爬取哔哩哔哩up主信息和投稿视频6. css列表标签list与表格标签table详解7. PHP中的闭包function() use() {}使用场景和技巧8. .net core 3.1 Redis安装和简单使用9. Ajax 请求队列解决方案并结合elementUi做全局加载状态10. Xpath语法格式总结

网公网安备