vant List列表左滑根据这一条数据的id删除
要实现在Vant List列表中左滑删除一条数据,可以按照以下步骤进行:
- 在Vue的组件中,定义一个包含多个数据对象的数组,每个数据对象包含一个id字段用于标识数据。
- 在Vant List组件中,使用v-for指令遍历数据数组,展示每个数据对象的内容,并绑定左滑删除事件。
- 在左滑删除事件处理函数中,获取当前被左滑的数据对象的id。
- 使用id在数据数组中找到对应的数据对象,并将其从数组中移除。
- 在移除数据对象后,Vant List组件会重新渲染,显示更新后的数据列表。
下面是一个示例代码:
<template>
<van-list>
<van-cell
v-for="item in dataList"
:key="item.id"
:title="item.name"
is-link
@click="handleClick(item)"
>
<template #extra>
<van-swipe-cell left-width="65" @delete="handleDelete(item.id)">
<div class="delete-btn">删除</div>
</van-swipe-cell>
</template>
</van-cell>
</van-list>
</template>
<script>
export default {
data() {
return {
dataList: [
{ id: 1, name: '数据1' },
{ id: 2, name: '数据2' },
{ id: 3, name: '数据3' }
]
};
},
methods: {
handleDelete(id) {
const index = this.dataList.findIndex(item => item.id === id);
if (index !== -1) {
this.dataList.splice(index, 1);
}
},
handleClick(item) {
// 处理点击事件
}
}
};
</script>
<style scoped>
.delete-btn {
width: 65px;
height: 100%;
line-height: 44px;
text-align: center;
color: #fff;
background-color: #f4333c;
}
</style>
在上述示例代码中,通过在van-swipe-cell组件上绑定@delete事件,来实现左滑删除的功能。在handleDelete方法中,通过findIndex方法找到要删除的数据对象在数据数组中的索引,并使用splice方法将其从数组中移除
AI 优化版
原文地址:https://www.cveoy.top/t/topic/iMQL 著作权归作者所有。请勿转载和采集!