Home >Web Front-end >uni-app >How does uniapp implement the left swipe to delete product function?

How does uniapp implement the left swipe to delete product function?

PHPz
PHPzOriginal
2023-04-17 14:16:001287browse

With the development of mobile Internet, e-commerce apps are becoming more and more popular, and shopping is becoming more and more convenient. In some e-commerce apps, users can operate products through the sliding interface, such as swiping left to delete products. So, how to swipe left to delete items in uniapp? This article will introduce it to you in detail.

  1. First of all, it needs to be made clear that uniapp is a development framework based on Vue.js, so our component implementation will be more Vue.js-based. In Vue.js, we generally use the v-for instruction and arrays for dynamic rendering. Therefore, we can also use a similar method to render our product list in uniapp.
  2. Next, we need to implement the function of swiping left to delete products. We can use the movable-view component provided by uni-app. This component can implement dragging effects on the x-axis. In the movable-view component, we need to set the following properties of the movable-view component:
<movable-view 
  class="swiper-item" 
  x="{{item.x}}" 
  animation="true" 
  direction="horizontal" 
  damping="80" 
  friction="2" 
 ></movable-view>

Among them, x is the position of the movable-view component, and the unit is rpx. In our product list, each product should be a movable-view component.

  1. In the movable-view component, we need to add a delete button, which should be displayed when the movable-view component slides left beyond a certain distance. In vue.js, we can use the v-if directive to dynamically control the display and hiding of elements, and in uniapp, we can also use conditional rendering to achieve this.
  2. When the user clicks the delete button, we need to remove the current product. In Vue.js, we can achieve this through the splice method of the array. Similarly, this method can also be used in uniapp. We can use a custom event to listen for the click event of the delete button and trigger the event in the child component to delete the current product. See the following sample code for specific operations:
<!-- 让商品列表动态渲染 -->
<view v-for="(item, index) in list" :key="index">
   <!-- 判断商品是否被选中,选中时改变背景颜色 -->
   <movable-view 
     :class="{
        &#39;swiper-item-active&#39;: item.active
      }" 
     @change="handleChange(index, $event)" 
     @touchend="handleTouchEnd(index, $event)" 
     x="{{item.x}}" 
     animation="true" 
     direction="horizontal" 
     damping="80" 
     friction="2" 
     :style="{left: item.active ? &#39;-200rpx&#39; : 0}"
   >
     <view class="swiper-wrapper">
       <view class="image-wrapper">
         <image :src="item.image"></image>
       </view>
       <view class="content-wrapper">
         <view class="title">{{item.title}}</view>
         <view class="price">{{item.price}}</view>
         <view class="number">{{item.number}}</view>
       </view>
       <!-- 绑定删除操作 -->
       <view 
         class="delete-btn" 
         v-if="item.active" 
         @click="$emit(&#39;delete&#39;, index)"
       >删除</view>
     </view>
   </movable-view>
 </view>
 
<script>
  export default {
    data() {
      return {
        list: [
          {
            title: '商品1',
            image: '',
            price: 100,
            number: 1,
            active: false,
            x: 0
          },
          {
            title: '商品2',
            image: '',
            price: 200,
            number: 1,
            active: false,
            x: 0
          }
        ]
      }
    },
    methods: {
      // 左滑删除商品
      handleChange(index, event) {
        // 获取movable-view组件的位置信息
        const { detail } = event;
        const x = detail.x;
        this.list[index].x = x;
        // 当移动距离超过200rpx时,显示删除按钮
        if (x <= -200) {
          this.list[index].active = true;
        } else {
          this.list[index].active = false;
        }
      },
      // 停止触摸事件
      handleTouchEnd(index, event) {
        const { detail } = event;
        const x = detail.x;
        // 当用户放手时,如果movable-view组件位置小于-200rpx,则直接删除该商品
        if (x <= -200) {
          this.list.splice(index, 1);
        } else {
          // 否则,商品位置复位
          this.list[index].x = 0;
        }
        // 删除操作完成后,将所有商品的选中状态重置
        this.list.forEach((item) => {
          item.active = false;
        });
      }
    }
  }
</script>

Through the above sample code, we can implement the left-swipe to delete product function in uniapp. It should be noted that the product list here is just an example. In actual situations, we need to obtain the product list from the API and render it dynamically. At the same time, we also need to turn the deletion operation into an asynchronous operation, that is, when deleting an item, we need to send a request to the server. Here we only introduce the basic operations in uniapp, and the specific implementation needs further improvement.

The above is the detailed content of How does uniapp implement the left swipe to delete product function?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn