微信小程序删除滑块 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()
        }

    }
})
相关推荐
萧寂1732 小时前
微信小程序自定义轮播图指示点样式
微信小程序·小程序
SuperherRo10 小时前
自动化反编译微信小程序工具-e0e1-wx
微信小程序·自动化·hook·反编译
R_yy11 小时前
微信小程序开发——视频播放实现(本地视频或者云端视频均可)
前端·微信小程序·小程序
京河小蚁13 小时前
解决Moodo调节心情模块-大声喊出来无法测量出音频分贝
微信小程序·音视频·ai编程·cursor
低代码布道师15 小时前
加油站小程序实战教程03站点管理
低代码·小程序
蒜香拿铁16 小时前
小程序参数获取方式
前端·微信小程序
V+zmm1013418 小时前
智慧物流小程序(论文源码调试讲解)
java·数据库·微信小程序·小程序·毕业设计
霍格沃兹测试开发学社测试人社区18 小时前
性能测试丨微信小程序性能优化指南
性能优化·微信小程序·小程序
猿毕设21 小时前
【FL0100】基于SSM微信小程序的走失人员的报备平台
java·spring boot·后端·python·微信小程序·小程序
猿毕设21 小时前
【FL0091】基于SSM和微信小程序的社区二手物品交易小程序
java·spring boot·后端·python·微信小程序·小程序