vant List列表van-swipe-cell左滑根据这一条数据的id删除
要实现根据id删除一条数据,可以按照以下步骤操作:
- 在需要使用van-swipe-cell组件的页面或组件中,引入van-swipe-cell组件和List组件。
import { List, Cell, SwipeCell } from 'vant';
- 在data中定义一个数组用来存储数据列表,并为每一条数据添加一个唯一的id。
data() {
return {
dataList: [
{ id: 1, name: '数据1' },
{ id: 2, name: '数据2' },
{ id: 3, name: '数据3' },
// ...
]
};
},
- 在模板中使用List组件来展示数据列表,并使用SwipeCell组件对每一条数据进行左滑删除操作。
<van-list>
<van-cell v-for="item in dataList" :key="item.id">
<van-swipe-cell @delete="onDelete(item.id)">
<!-- 显示数据内容 -->
<template #right>
<van-button type="danger" text="删除" />
</template>
</van-swipe-cell>
</van-cell>
</van-list>
- 在methods中定义一个onDelete方法来处理删除操作,并根据传入的id从dataList中删除对应的数据。
methods: {
onDelete(id) {
const index = this.dataList.findIndex(item => item.id === id);
if (index !== -1) {
this.dataList.splice(index, 1);
}
}
}
通过以上的步骤,就可以实现根据id删除一条数据的功能。当用户左滑某一条数据并点击删除按钮时,会触发onDelete方法,并根据传入的id删除对应的数据
原文地址: https://www.cveoy.top/t/topic/iMRD 著作权归作者所有。请勿转载和采集!