小程序实现上下移动切换位置

小程序实现上下移动切换位置

<view wx:for="{{companyData}}" wx:key="{{item.id}}"> <view>{{item.name}}</view> <view wx:if="{{index != 0}}" data-index="{{index}}" bindtap="topClick"> <image src=""></image> </view> <view wx:if="{{index != companyData.length-1}}" data-index="{{index}}" bindtap="bottomClick"> <image src=""></image> </view> </view>

data{ companyData:[ { id:0, name:'产品1' }, { id:1, name:'产品2' }, { id:2, name:'产品3' }, { id:3, name:'产品4' }, { id:4, name:'产品5' } ] }

//位置向上移动 topClick: function(e) { var that = this var index = e.currentTarget.dataset.index var one = that.data.companyData[index] var two = that.data.companyData[index - 1] var index2 = index - 1 var data1 = "companyData[" + index + "]" var data2 = "companyData[" + index2 + "]" that.setData({ [data1]: two, [data2]: one }) }, // 位置向下移动 bottomClick: function(e) { var that = this var index = e.currentTarget.dataset.index var one = that.data.companyData[index] var two = that.data.companyData[index + 1] var index2 = index + 1 var data1 = "companyData[" + index + "]" var data2 = "companyData[" + index2 + "]" that.setData({ [data1]: two, [data2]: one }) }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/335ac6f309304f4c4d005bb8a7be39d6.html