一、背景
一般运用在多个商品列表中
二、功能
主要有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>