在項目中常常有刪除的功能,那小程序是如何實現這個刪除的功能呢?思路就是找到指定的內容,把內容從數組中刪除掉。小程序
樣式你們都懂,因此沒寫,具體代碼以下:數組
<view wx:for="{{list}}">
<view class="bigBox" >列表內容</view>
<view class="delete" catchtap="delcon" data-indexdel="{{index}}" >刪除</view>
</view>
<!--在刪除的按鈕上自定義屬性 indexdel ,用於獲取需刪除的內容的下標位置 -->
var app = getApp(); Page({ data: { list:[1,2,3,4,5], }, delcon:function(e){ var index = e.currentTarget.dataset.indexdel; //獲取自定義的內容下標值
var list = this.data.list; //獲取內容列表
wx.showModal({ title: '是否肯定刪除內容?', success: function (res) { if (res.confirm) { //點擊肯定後
list.splice(index, 1); //截取指定的內容
this.setData({ //從新渲染列表
list:list }) } } }) } })
總結:以上內容供學習總結分享,有什麼不對的地方或可優化的地方,歡迎各位多多指教。app