微信小程序删除滑块 SwiperCell 自动收起 Van weapp van-swipe-cell 滑块自动收起 点击页面也自动收起滑块

  1. 在当前页面整个 view 中 给页面绑定 点击事件bindtap="onSwipeCellPage"
  2. 给 van-swipe-cell 组件设置 id (for循环可以添加 id="swip-cell-{``{item.id}}"
  3. van-swipe-cell 组件 添加属性 当用户打开滑块时触发 bind:open="swiperCellOpen"
  4. van-swipe-cell 组件 添加属性 点击滑动单元格时触发的事件 bind:click="onSwipeCellClick"
  5. 需要对单元格实例数组进行遍历,遍历以后获取每一个实例,让每一个实例调用 close 方式即可

示例

  1. 新建文件夹 SwiperCell.js
javascript 复制代码
Page({
    data: {
        swipeCellQueue: [], //用来存储滑动单元格实例 
    },

    // 当用户打开滑块时触发
    swiperCellOpen(event) {
        // 获取单元格实例
        const instance = this.selectComponent(`#${event.target.id}`)
        // 将实例追加到数据中
        this.data.swipeCellQueue.push(instance)
    },
    // 给页面绑定 点击事件
    onSwipeCellPage() { this.onSwipeCellCommonClick() },
    // 点击滑动单元格时触发的事件
    onSwipeCellClick() { this.onSwipeCellCommonClick() },
    onSwipeCellCommonClick() {
        // 需要对单元格实例数组进行遍历,遍历以后获取每一个实例,让每一个实例调用 close 方式即可
        this.data.swipeCellQueue.forEach((instance) => {
            instance.close()
        })
        // 将滑动单元格数组重置为空
        this.data.swipeCellQueue = []
    }
})
  1. 然后在使用文件 wxml 中 最外层的 view 绑定 页面点击事件 bindtap="onSwipeCellPage"
  2. 给 swipe-cell 绑定一个 id <van-swipe-cell class="goods-swipe" right-width="{``{ 65 }}" id="swipe-cell-{``{item.id}}">
  3. 给 swipe-cell 绑定open和click事件 <van-swipe-cell class="goods-swipe" right-width="{``{ 65 }}" id="swipe-cell-{``{item.id}}" bind:open="swiperCellOpen" bind:click="onSwipeCellClick">
  4. 在 使用文件 js 中 引入 Behaviors
  5. 然后在 onHide 方法 引入 在页面隐藏的时候,需要让删除滑块自动弹回
javascript 复制代码
import { ComponentWithStore } from 'mobx-miniprogram-bindings'
import { userStore } from '@/stores/userStore'
import { reqDelCartGoods } from "@/api/cart"

import { swipeCellBehavior } from "@/behaviors/swiperCell"
const computedBehavior = require("miniprogram-computed").behavior
ComponentWithStore({
    behaviors: [ swipeCellBehavior],
    storeBindings: {
        store: userStore,
        fields: ['token']
    },
    data: {
        cartList: []
    },
    methods: {
        //  如果使用 Compoent 方法来构建页面
        // 生命周期钩子函数 需要在 methods 中才可以
        // 删除商品
        async delCartGoods(evnet) {
        	// 要删除的id
            let { id } = evnet.currentTarget.dataset
            // 自己封装的 modal方法
            const isOk = await wx.modal({
                title: '提示',
                content: "您确定要删除吗?"
            })

            if (!isOk) {
                return
            }
            const res = await reqDelCartGoods(id)
            if (res.code === 200) {
                ....
            }

        },
        onHide() {
            this.onSwipeCellCommonClick()
        }

    }
})
相关推荐
发财北3 分钟前
线上交友APP怎么开发?
小程序
2501_9159214313 分钟前
Bundle Id 创建与管理的工程化方法,一次团队多项目协作中的流程重构
服务器·ios·小程序·重构·https·uni-app·iphone
少云清34 分钟前
【功能测试】4_小程序项目 _Ego微商小程序测试点分析
功能测试·小程序
说私域2 小时前
直播带货的困境与突破:基于“开源AI智能名片链动2+1模式S2B2C商城小程序”的创新研究
人工智能·小程序·开源
2501_915909062 小时前
深度解析 iOS 内存占用,构建多工具协同的内存诊断、监控与优化体系
android·ios·小程序·https·uni-app·iphone·webview
菜鸟学Python2 小时前
零基础用AI编程开发微信小程序-开始篇
微信小程序·小程序·ai编程
q_19132846953 小时前
基于Springboot2+Vue2+uniapp的单商家在线点餐外卖小程序
vue.js·spring boot·mysql·小程序·uni-app·计算机毕业设计
2501_915918414 小时前
iOS CPU 使用率深度分析,多工具协同定位高占用瓶颈的工程化方法
android·ios·小程序·https·uni-app·iphone·webview
2501_915106324 小时前
如何防止资源文件被替换?一套针对 iOS App 的多层资源安全方案
android·安全·ios·小程序·uni-app·iphone·webview
2501_915918414 小时前
IPA 一键加密工具的真实工程化实践 构建可自动化、可扩展的 iOS 应用安全体系
android·安全·ios·小程序·uni-app·自动化·iphone