当前位置:网站首页>[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
边栏推荐
- Ajout, suppression et modification des données du tableau [MySQL] (DML)
- Sum of two array numbers
- [tcapulusdb knowledge base] tcapulusdb viewing process status introduction
- Simply Untyped Lambda Calculus的实现
- Record (III)
- C use s7 Net connected to Siemens s1200plc, C # directly connected to Siemens PLC
- 自己搞了一个相亲软件的源码,用兴趣的可以聊聊
- leetcode 130. Surrounded regions (medium)
- (十一)TableView
- Model construction of mmdetection
猜你喜欢
![[debug] could not find ref wiht POC XXX](/img/01/5fdbd3e9902458be754280464bd923.png)
[debug] could not find ref wiht POC XXX

Kdd2022 | neural network compression of depth map based on antagonistic knowledge distillation
![[tcapulusdb knowledge base] tcapulusdb machine initialization and launch introduction](/img/7b/8c4f1549054ee8c0184495d9e8e378.png)
[tcapulusdb knowledge base] tcapulusdb machine initialization and launch introduction

TcaplusDB君 · 行业新闻汇编(一)

How to stimulate the vitality and driving force of cultural innovation

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

leetcode:333. 最大 BST 子树

【Xpath】使用following-sibling获取后面的同级节点

What are MySQL clustered indexes and nonclustered indexes?

C#使用S7.net连接西门子S1200PLC,C#直接连接西门子PLC
随机推荐
leetcode 130. Surrounded regions (medium)
[MySQL] Table constraints
README
Add, delete, query and modify MySQL table structure (DDL)
Ability to deliver packages within D days [abstract class dichotomy -- Abstract judgment method]
TypeScript - 声明文件和内置对象
Matlab - 演化博弈论实现
【C#】overide可重写从更高父级继承来的virtual方法
torch_geometric
Matlab - Implementation of evolutionary game theory
【Xpath】使用following-sibling获取后面的同级节点
Solution de gestion de la zone pittoresque intelligente pour la réunion des baleines
[tcapulusdb knowledge base] tcapulusdb daily inspection introduction
【生成对抗网络学习 其一】经典GAN与其存在的问题和相关改进
自己做了个相亲交友App,有兴趣的朋友可以看看
Sealem Finance打造Web3去中心化金融平台基础设施
Ajout, suppression et modification des données du tableau [MySQL] (DML)
(11) Tableview
[generation confrontation network learning part I] classic Gan and its existing problems and related improvements
(十一)TableView