uni-app 实现左滑删除两种数据结构(超详细附源码)

我们先来看第一种简单的二维数组

来张效果图

实现代码

<template>
	<view class="content unify-margin">
		<view v-if="list.length !== 0">
			<!-- 间隔 -->
			<uni-interval height="8px"></uni-interval>

			<view class="item unify-relative unify-padding unify-background-color unify-radius"
				v-for="(item,index) in list" :key="index" @touchstart="drawStart" :data-index="index"
				@touchmove="drawMove" @touchend="drawEnd" :style="'right:'+item.right+ 'rpx'">
				<view class="item-nk unify-flex" @tap="tapCut(item)">
					<image class="image unify-block unify-radius" :src="item.image || errorImage" mode=""></image>
					<view class="right unify-relative unify-weight">
						<view class="title unify-font-size-line-astrict">{{item.name}}</view>
						<view class="right-data unify-flex unify-absolute">
							<view class="right-score">
								4.8分
								<text class="right-appraise">| 66人评价</text>
							</view>
							<view class="right-distance">1km</view>
						</view>
					</view>
				</view>

				<view class="remove" @tap="tapDel(index)">删除</view>
			</view>

			<!-- 间隔 -->
			<uni-interval height="30px"></uni-interval>

			<!-- 加载更多 -->
			<uni-loading-more :loadingType="loadingType" :contentText="contentText"></uni-loading-more>

			<!-- 间隔 -->
			<uni-interval height="30px"></uni-interval>
		</view>

		<!-- 没有数据 -->
		<uni-null desc="没有收藏哦! 赶紧去收藏吧~" v-else></uni-null>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				errorImage: this.$mAssetsPath.errorImage, // 图片加载失败填充
				list: [{
					image: '',
					name: '食悦阁中餐食补',
				}],
				delBtnWidth: 120, // 滑动距离
				contentText: {
					contentdown: "上拉显示更多",
					contentrefresh: "正在加载...",
					contentnomore: '只有这么多了'
				},
				pageNum: 1, // 页码
				pageSize: 10, // 每页显示数量
				loadingType: 2, // 加载动画
			}
		},
		methods: {
			// 切换函数处理事件
			tapCut(e) {
				for (var index in this.list) { // 滑动初始化
					this.$set(this.list[index], 'right', 0);
				}
			},

			// 删除函数处理事件
			tapDel(e) {
				uni.showModal({
					title: '提示',
					content: '确认删除该收藏?',
					confirmText: '确认',
					success: res => {
						if (res.confirm) {
							let list = this.list;
							list.forEach((item, index) => {
								if (e === index) {
									list.splice(index, 1)
								}
							})
							this.list = list;
							this.$mHelper.toast('删除成功')
						}
					}
				})
			},

			//开始触摸滑动
			drawStart(e) {
				var touch = e.touches[0];
				this.startX = touch.clientX;
			},
			//触摸滑动
			drawMove(e) {
				for (var index in this.list) {
					this.$set(this.list[index], 'right', 0);
				}
				var touch = e.touches[0];
				var item = this.list[e.currentTarget.dataset.index];
				var disX = this.startX - touch.clientX;
				if (disX >= 20) {
					if (disX > this.delBtnWidth) {
						disX = this.delBtnWidth;
					}
					this.$set(this.list[e.currentTarget.dataset.index], 'right', disX);
				} else {
					this.$set(this.list[e.currentTarget.dataset.index], 'right', 0);
				}
			},
			//触摸滑动结束
			drawEnd(e) {
				var item = this.list[e.currentTarget.dataset.index];
				if (item.right >= this.delBtnWidth / 2) {
					this.$set(this.list[e.currentTarget.dataset.index], 'right', this.delBtnWidth);
				} else {
					this.$set(this.list[e.currentTarget.dataset.index], 'right', 0);
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	$image: 110rpx;
	$text-size: 28rpx;

	.content {

		.item {
			height: 142rpx;
			margin-top: 9px;


			.item-nk {
				height: inherit;
				align-items: center;
				justify-content: space-between;
			}

			.image {
				width: $image;
				height: $image;
			}

			.right {
				width: 78%;
				height: $image;

				.title {
					font-size: $text-size;
				}

				.right-data {
					width: 100%;
					align-items: center;
					justify-content: space-between;
					bottom: 0;

					.right-score {
						font-size: $text-size;
						color: $uni-text-color-orange;

						.right-appraise {
							font-size: 24rpx;
							color: $uni-text-color-grey;
							margin-left: 10rpx;
							font-weight: 400;
						}
					}

					.right-distance {
						font-size: 24rpx;
						color: $uni-text-color-grey;
						font-weight: 400;
					}
				}
			}

			// 删除
			.remove {
				width: 108rpx;
				text-align: center;
				height: inherit;
				background-color: #FF0000;
				color: white;
				position: absolute;
				right: -154rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				border-radius: 20rpx 0 0 20rpx;
				font-size: 24rpx;
				top: 0;
				bottom: 0;
				margin: auto;
			}
		}
	}
</style>

第二种三维数组

来张效果图

实现代码

<template>
	<view class="content unify-margin">
		<view v-if="list.length !== 0">
			<view class="outer-sphere" v-for="(item1,index1) in list" :key="index1">
				<!-- 时间 -->
				<view class="time">{{item1.time}}</view>

				<uni-interval height="5px"></uni-interval>

				<view class="item unify-relative unify-padding unify-background-color unify-radius"
					v-for="(item2,index2) in item1.array" :key="index2" @touchstart="drawStart" :data-index1="index1"
					:data-index2="index2" @touchmove="drawMove" @touchend="drawEnd"
					:style="'right:'+item2.right+ 'rpx'">
					<view class="item-nk unify-flex" @tap="tapCut(item2)">
						<image class="image unify-block unify-radius" :src="item2.image || errorImage" mode=""></image>
						<view class="right unify-relative unify-weight">
							<view class="title unify-font-size-line-astrict">{{item2.name}}</view>
							<view class="right-data unify-flex unify-absolute">
								<view class="right-score">
									4.8分
									<text class="right-appraise">| 66人评价</text>
								</view>
								<view class="right-distance">1km</view>
							</view>
						</view>
					</view>

					<view class="remove" @tap="tapDel(index2)">删除</view>
				</view>
			</view>

			<!-- 间隔 -->
			<uni-interval height="30px"></uni-interval>

			<!-- 加载更多 -->
			<uni-loading-more :loadingType="loadingType" :contentText="contentText"></uni-loading-more>

			<!-- 间隔 -->
			<uni-interval height="30px"></uni-interval>
		</view>

		<!-- 没有数据 -->
		<uni-null desc="没有收藏哦! 赶紧去收藏吧~" v-else></uni-null>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				errorImage: this.$mAssetsPath.errorImage, // 图片加载失败填充
				list: [{
					time: '12-15',
					array: [{
						image: '',
						name: '食悦阁中餐食补',
					}, {
						image: '',
						name: '食悦阁中餐食补',
					}, {
						image: '',
						name: '食悦阁中餐食补',
					}]
				}, {
					time: '12-15',
					array: [{
						image: '',
						name: '食悦阁中餐食补',
					}, {
						image: '',
						name: '食悦阁中餐食补',
					}, {
						image: '',
						name: '食悦阁中餐食补',
					}]
				}],
				delBtnWidth: 120, // 滑动距离
				contentText: {
					contentdown: "上拉显示更多",
					contentrefresh: "正在加载...",
					contentnomore: '只有这么多了'
				},
				pageNum: 1, // 页码
				pageSize: 10, // 每页显示数量
				loadingType: 2, // 加载动画
			}
		},
		methods: {
			// 切换函数处理事件
			tapCut(e) {
				let list = this.list;
				for (var index1 in list) { // 滑动初始化
					for (var index2 in list[index1].array) {
						this.$set(list[index1].array[index2], 'right', 0);
					}
				}
			},

			// 删除函数处理事件
			tapDel(e) {
				uni.showModal({
					title: '提示',
					content: '确认删除该收藏?',
					confirmText: '确认',
					success: res => {
						if (res.confirm) {
							let list = this.list;
							list.forEach((item1, index1) => {
								item1.array.forEach((item2, index2) => {
									if (e === index2) {
										item1.array.splice(index2, 1)
									}
								})
								if (item1.array.length === 0) {
									list.splice(index1, 1)
								}
							})
							this.list = list;
							this.$mHelper.toast('删除成功')
						}
					}
				})
			},

			//开始触摸滑动
			drawStart(e) {
				var touch = e.touches[0];
				this.startX = touch.clientX;
			},
			//触摸滑动
			drawMove(e) {
				for (var index1 in this.list) {
					for (var index2 in this.list[index1].array) {
						this.$set(this.list[index1].array[index2], 'right', 0);
					}
				}
				var touch = e.touches[0];
				var item = this.list[e.currentTarget.dataset.index1].array[e.currentTarget.dataset.index2];
				var disX = this.startX - touch.clientX;
				if (disX >= 20) {
					if (disX > this.delBtnWidth) {
						disX = this.delBtnWidth;
					}
					this.$set(this.list[e.currentTarget.dataset.index1].array[e.currentTarget.dataset.index2], 'right',
						disX);
				} else {
					this.$set(this.list[e.currentTarget.dataset.index1].array[e.currentTarget.dataset.index2], 'right', 0);
				}
			},
			//触摸滑动结束
			drawEnd(e) {
				var item = this.list[e.currentTarget.dataset.index1].array[e.currentTarget.dataset.index2];
				if (item.right >= this.delBtnWidth / 2) {
					this.$set(this.list[e.currentTarget.dataset.index1].array[e.currentTarget.dataset.index2], 'right',
						this.delBtnWidth);
				} else {
					this.$set(this.list[e.currentTarget.dataset.index1].array[e.currentTarget.dataset.index2], 'right', 0);
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	$image: 110rpx;
	$text-size: 28rpx;

	.content {

		.outer-sphere:first-child {
			margin-top: 17px;
		}

		.outer-sphere {
			margin-top: 26px;
		}

		.time {
			font-size: $text-size;
			color: $uni-text-color-grey;
		}

		.item {
			height: 142rpx;
			margin-top: 9px;


			.item-nk {
				height: inherit;
				align-items: center;
				justify-content: space-between;
			}

			.image {
				width: $image;
				height: $image;
			}

			.right {
				width: 78%;
				height: $image;

				.title {
					font-size: $text-size;
				}

				.right-data {
					width: 100%;
					align-items: center;
					justify-content: space-between;
					bottom: 0;

					.right-score {
						font-size: $text-size;
						color: $uni-text-color-orange;

						.right-appraise {
							font-size: 24rpx;
							color: $uni-text-color-grey;
							margin-left: 10rpx;
							font-weight: 400;
						}
					}

					.right-distance {
						font-size: 24rpx;
						color: $uni-text-color-grey;
						font-weight: 400;
					}
				}
			}

			// 删除
			.remove {
				width: 108rpx;
				text-align: center;
				height: inherit;
				background-color: #FF0000;
				color: white;
				position: absolute;
				right: -154rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				border-radius: 20rpx 0 0 20rpx;
				font-size: 24rpx;
				top: 0;
				bottom: 0;
				margin: auto;
			}
		}
	}
</style>

 两种相差无异,只不过是取值不同

如果对你有用,关注一下博主的小程序,登录一下给予支持,以后有什么开源好用的源码都会上传到小程序 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值