Uniapp-左滑删除

一、背景

一般运用在多个商品列表中

 二、功能

 主要有touchstart、touchmove、touchend、和一个动态绑定样式 :style

在方法中定义:

 

完整版代码 -- 与截图样式不同, 但功能一样:

<template>
	<view>
		<view style="height: 100vh;">
			<view class="bg-white" >
				<text style="font-size: 80rpx; margin-left: 87%; color: orangered;" cursor @click="add"> + </text>
				<view class="flex justify-around padding-sm" style="border-bottom: 2rpx solid #ccc;">
					<view class="text-center text-bold text-xxl">
						<view>{{ fulldate }}</view>
					</view>
				</view>
			</view>
			
			<!-- 事件展示 -->
			<view class="carList" v-if="dataList.length != 0">
				<view class="order-item flex" v-for="(item, index) in dataList" :key="index" :data-index="index"
					@touchstart="tcouchStart" @touchmove="tcouchMove" @touchend="tcouchEnd"
					:style="'right:'+item.right+'px'">
					<view style="display: flex; overflow: hidden; align-items: center;" @click="edit(item)">
						<view style="display: flex; align-items: center; overflow: hidden;">
							<view class="padding-sm">
								<image src="../../static/dot.png" style="width: 30rpx; height:30rpx;" />
							</view>
							<view class="item-info">
								<view class="text-bold text-xl margin-top-sm">{{ item.title }}</view>
								<view style="color: gray; margin-top: 20rpx;">{{ item.content }}</view>
								<view style="color: gray; margin-top: 20rpx;">{{ item.update_time }}</view>
							</view>
						</view>
					</view>
					<view class="remove" @click="itemDelClick(item.id)">删除</view>
				</view>
			</view>
			
			<view v-else style="text-align: center; margin-top: 50%;">
				<image src="../../static/empty.png" style="width: 80rpx; height: 80rpx;"></image>
				<view>暂无数据</view>
			</view>
		</view>
		
		<!-- 普通弹窗 -->
		<uni-popup ref="popup" background-color="#fff" type="bottom">
			<view class="flex justify-between align-center padding-sm">
				<view class="text-lg" @click="cancel">取消</view>
				<view class="text-bold text-xl">新建日程</view>
				<view class="text-bold text-lg" style="color: orangered;" @click="addInfo">添加</view>
			</view>
			<view class="popup-content">
				<view>
					<view class="text-bold padding-tb-sm">标题:</view>
					<uni-easyinput v-model="title" focus placeholder="请输入标题"></uni-easyinput>
				</view>
				<view class="margin-top-sm">
					<view class="text-bold padding-tb-sm">内容:</view>
					<!-- <uni-easyinput v-model="content" focus placeholder="请输入内容" style="height: 300rpx"></uni-easyinput> -->
					<textarea v-model="content" placeholder="请输入内容" class="content_input"></textarea>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import { listing } from "../../api/api.js"
	export default {
		data() {
			return {
				fulldate: '',
				dataList: [],
				title: '',
				content: '',
				id: '',
				delBtnWidth: 60,
				startX: '',
				selected: [],
			}
		},
		onLoad(props) {
			if(!!props.fulldate){
				this.fulldate = props.fulldate
				this.viewData()
			}
			this.selected = uni.getStorageSync('selected')
		},
		methods:{
			add(){
				this.id = ''
				this.title = ''
				this.content = ''
				this.$refs.popup.open()
			},
			cancel(){
				this.$refs.popup.close()
			},
			addInfo(){
				listing("miniPro/schedule", {
					date: this.fulldate, 
					title: this.title,
					content: this.content,
					id: !!this.id ? this.id : ''
				}, !!this.id ? 'PUT':'POST', true, true)
				.then((res) => {
					if (res.code == 200) {
						this.$refs.popup.close()
						this.viewData()
						if(this.selected.indexOf(this.fulldate) == -1){
							this.selected.push(this.fulldate)
						}
						
						uni.setStorageSync('selected', this.selected)
					}
				})
			},
			edit(item){
				this.title = item.title
				this.content = item.content
				this.id = item.id
				this.$refs.popup.open()
			},
			viewData(){
				listing("miniPro/schedule", {
					date: this.fulldate
				}, 'GET', false, false)
				.then((res) => {
					if (res.code == 200) {
						this.dataList = res.data
					}
				})
			},
			// 删除事件
			itemDelClick(id){
				let _this = this
				uni.showModal({
					title: '温馨提示',
					content: '是否确定删除该日程',
					success: function (res) {
						if (res.confirm) {
							listing("miniPro/schedule", {
								id: id
							}, 'DELETE', true, true)
							.then((res) => {
								if(res.code == 200){
									_this.viewData()
									// 说明记录了多条日程 -- 那么就不用删除,否则就是删除
									if(_this.dataList.length ==1){
										_this.selected = _this.selected.filter(i=>{return i != _this.fulldate})
										uni.setStorageSync('selected', _this.selected)
									}
								}
							})
						}
					}
				})
			},
			tcouchStart(e){
				var touch = e.touches[0];
				this.startX = touch.clientX;
			},
			tcouchMove(e){
				for (var index in this.dataList) {
					this.$set(this.dataList[index], 'right', 0);
				}
				var touch = e.touches[0];
				var item = this.dataList[e.currentTarget.dataset.index];
				var disX = this.startX - touch.clientX;
				if (disX >= 20) {
					if (disX > this.delBtnWidth) {
						disX = this.delBtnWidth;
					}
					this.$set(this.dataList[e.currentTarget.dataset.index], 'right', disX);
				} else {
					this.$set(this.dataList[e.currentTarget.dataset.index], 'right', 0);
				}
			},
			tcouchEnd(e){
				var item = this.dataList[e.currentTarget.dataset.index];
				if (item.right >= this.delBtnWidth / 2) {
					this.$set(this.dataList[e.currentTarget.dataset.index], 'right', this.delBtnWidth);
				} else {
					this.$set(this.dataList[e.currentTarget.dataset.index], 'right', 0);
				}
			},
		}
	}
</script>

<style lang="scss">
	.popup-content {
		align-items: center;
		justify-content: center;
		padding: 30rpx;
		height: 600rpx;
		background-color: #fff;
	}
	
	.carList {
		border-radius: 20rpx;
		padding: 10rpx 2% 10rpx 3%;
	}
	.order-item {
		width: 100%;
		display: flex;
		position: relative;
		margin: 20rpx auto;
		align-items: right;
		flex-direction: row;
		background: white;
		border-radius: 20rpx;
	}
	
	.item-info {
		font-size: 34rpx;
		color: #333;
		padding: 10rpx 20rpx;
		position: relative;
		overflow: hidden;
		flex: 1;
	}
	.remove {
		width: 120rpx;
		height: 100%;
		background-color: orangered;
		color: #FFFFFF;
		position: absolute;
		top: 0;
		right: -140rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 32rpx;
		border-radius: 20rpx;
	}
	.content_input {
		height: 240rpx;
		width: 100%;
		border: 2rpx solid #2979ff;
		border-radius: 10rpx;
		padding: 20rpx;
	}
</style>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Uniapp 中实现购物车左滑删除商品的功能,你可以使用一些组件或库来实现。以下是一种常见的实现方式: 1. 首先,你需要使用 `<swiper>` 组件作为购物车列表的容器,并设置 `direction` 属性为 `horizontal`,使其能够横向滑动。 2. 在每个购物车商品项中,使用 `<swiper-item>` 组件包裹内容,并设置左滑删除的按钮。 3. 在左滑删除按钮上绑定一个事件,当用户点击按钮时,触发相应的事件处理函数。 4. 在事件处理函数中,根据商品的唯一标识(如商品ID)从购物车数组中找到对应的商品项,并将其从数组中移除。 5. 更新购物车数组后,可以选择将新的购物车数据存储在本地或发送到服务端进行更新。 下面是一个简单的示例代码,帮助你理解如何实现购物车左滑删除商品的功能: ```vue <template> <div> <swiper class="cart-swiper" direction="horizontal"> <swiper-item v-for="item in cartItems" :key="item.id"> <div class="cart-item"> <div class="item-content"> {{ item.name }} - {{ item.price }} </div> <div class="delete-btn" @click="removeFromCart(item.id)"> 删除 </div> </div> </swiper-item> </swiper> </div> </template> <script> export default { data() { return { cartItems: [ { id: 1, name: '商品1', price: 10 }, { id: 2, name: '商品2', price: 20 }, { id: 3, name: '商品3', price: 30 } ] } }, methods: { removeFromCart(itemId) { this.cartItems = this.cartItems.filter(item => item.id !== itemId); } } } </script> <style scoped> .cart-swiper { width: 100%; height: 100%; } .cart-item { display: flex; align-items: center; height: 100px; } .item-content { flex: 1; } .delete-btn { width: 100px; background-color: red; color: white; text-align: center; } </style> ``` 以上示例代码使用了 `<swiper>` 和 `<swiper-item>` 组件来实现左滑删除功能,需要注意的是,你可能还需要根据实际情况对样式进行调整。希望能帮到你!如果还有其他问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值