基于js实现数组相邻元素上移下移
网络编程 2021-07-04 15:02www.168986.cn编程入门
这篇文章主要介绍了基于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-width="80" align="center"> <template slot-scope="scope"> <div class="img_style"> <img src="@/assets/images/up_01.png" v-if="scope.$index == 0"> <img src="@/assets/images/up.png" @click="clickUp(scope.$index)" v-else> <img src="@/assets/images/down_01.png" v-if="scope.$index == tableData.length - 1"> <img src="@/assets/images/down.png" @click="clickDown(scope.$index)" v-else> </div> </template> </el-table-column>
注意:
1.思想就是在数组中交换两个元素的位置,使用splice()的替换;
2.上移是跟上一元素交换位置,下移是跟下一元素交换位置,不同体现在调用调换方法时传入的index参数不同。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持狼蚁SEO。
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程