当前位置:网站首页>[applet] vant sliding cell adds the function of clicking other positions to close automatically
[applet] vant sliding cell adds the function of clicking other positions to close automatically
2022-06-10 22:33:00 【micromicrofat】
Use... In projects today vant Of SwipeCell Sliding cells to delete shopping cart by sliding left , The simplified code is as follows
<view wx:for="{
{cart}}" wx:key="id">
<van-swipe-cell right-width="{
{ 65 }}" bind:open="onOpen">
<van-cell-group>
<van-cell title="{
{item.title}}" value="{
{item.value}}" />
</van-cell-group>
<view slot="right" class="right" bindtap="onDelete" data-id="{
{item.id}}"> Delete </view>
</van-swipe-cell>
</view>
.right{
background-color: red;
height: inherit;
width: 65px;
color: white;
display: flex;
align-items: center;
justify-content: center;
}
Page({
data: {
cart: [
{
id: "001", title: "iPhone", value: " mobile phone " },
{
id: "002", title: "MacBook", value: " The notebook " },
{
id: "003", title: "AirPods", value: " The headset " },
],
},
onDelete() {
console.log("delete", event.target.dataset.id);
},
onOpen() {
console.log("open");
},
});

But there's a problem : Clicking on a blank position cannot automatically close 
After checking, I found that the official did not provide this function , And some people have experienced similar problems
https://github.com/youzan/vant-weapp/issues/4660
https://blog.csdn.net/pannikin/article/details/118526160
The final solution is to open The instance of the cell is stored in the variable , When responding to a click event , Call instance methods one by one from instances in variables close
The code is as follows :
<!-- cart/cart.wxml -->
<view class="page-view" bindtap="onTap">
<view wx:for="{
{cart}}" wx:key="id">
<van-swipe-cell id="wx{
{item.id}}" right-width="{
{ 65 }}" data-id="{
{item.id}}" bind:open="onOpen">
<van-cell-group>
<van-cell title="{
{item.title}}" value="{
{item.value}}" />
</van-cell-group>
<view slot="right" class="right" bindtap="onDelete" data-id="{
{item.id}}"> Delete </view>
</van-swipe-cell>
</view>
</view>
page{
height: 100%;
}
.page-view{
height: 100%;
}
.right{
background-color: red;
height: inherit;
width: 65px;
color: white;
display: flex;
align-items: center;
justify-content: center;
}
Page({
data: {
cart: [
{
id: "001", title: "iPhone", value: " mobile phone " },
{
id: "002", title: "MacBook", value: " The notebook " },
{
id: "003", title: "AirPods", value: " The headset " },
],
selected: [],
},
onDelete(event) {
console.log("delete", event.target.dataset.id);
},
onOpen(event) {
console.log("open");
// according to id choice instance
let instance = this.selectComponent(`#${
event.target.id}`);
this.data.selected.push(instance);
},
onTap() {
console.log("tap");
// Cycle off
this.data.selected.forEach(function (instance) {
instance.close();
});
// Empty
this.data.selected = [];
},
});

You will find that in the process of obtaining instances , It's through the elements id In combination with this.selectComponent Method to find , The element id When rendering, use the... In the business data id. The reason for this is , It's all because open There is no... In the parameters of the event instance, If there is one, it can be written like this
...
onOpen(event) {
const {
position, instance } = event.detail;
this.data.selected.push(instance);
},
...
Unfortunately, this is just wishful thinking , The authorities do not support 
Reference resources :https://youzan.github.io/vant-weapp/#/swipe-cell
边栏推荐
- 修改SpriteMask 的 frontSortingLayer 变量
- Capacity expansion mechanism of ArrayList
- TcaplusDB君 · 行业新闻汇编(三)
- What are MySQL clustered indexes and nonclustered indexes?
- 鲸会务会议分享:大会难办怎么办?
- 【TcaplusDB知识库】TcaplusDB机器初始化和上架介绍
- TcaplusDB君 · 行业新闻汇编(五)
- CCF CSP 202109-2 非零段划分【100分】
- Simply Untyped Lambda Calculus的实现
- SQLServer数据库应用与开发:第九章上机
猜你喜欢
![[tcapulusdb knowledge base] tcapulusdb viewing process status introduction](/img/8d/fc454b23489edcd8a7528a0acd3a02.png)
[tcapulusdb knowledge base] tcapulusdb viewing process status introduction

Sealem Finance打造Web3去中心化金融平台基础设施

How to do well in the top-level design of informatization in the process of informatization upgrading of traditional enterprises

leetcode:333. Maximum BST subtree

鯨會務智慧景區管理解决方案

重排 (reflow) 与重绘 (repaint)

GMPNN:Drug-drug interaction prediction with learnable size-adaptive molecular substructures.

OC swift hybrid

Kdd2022 | neural network compression of depth map based on antagonistic knowledge distillation
![在D天内送达包裹的能力[抽象类二分--抽象判定方式]](/img/16/026c017e1c54d580fa2d237aa4605c.png)
在D天内送达包裹的能力[抽象类二分--抽象判定方式]
随机推荐
笔记(四)- 多线程
JVM运行时数据区
数组 删除数组中的重复项
Array plus one
自己搞了一个相亲软件的源码,用兴趣的可以聊聊
[tcapulusdb knowledge base] tcapulusdb daily inspection introduction
Add, delete, query and modify MySQL table structure (DDL)
【TcaplusDB知识库】TcaplusDB进程启动介绍
【TcaplusDB知识库】TcaplusDB刷新tbus通道介绍
Management solution for whale conference smart scenic spot
自己做了个相亲交友App,有兴趣的朋友可以看看
How small and micro enterprises build micro official websites at low cost
Array to sum the heights of rising intervals
[tcapulusdb knowledge base] tcapulusdb viewing process status introduction
Latex error: file ‘xxx. sty‘ not found
Model construction of mmdetection
Ceph分布式存储集群Pool资源池的概念以及使用
A number that appears only once in an array
CCF CSP 202109-3 脉冲神经网络
《暗黑破坏神不朽》数据库资料站地址 暗黑不朽资料库网址