1.添加排序按钮和触发事件
<button bindtap="order">按商品价格排序</button>
2.编写排序函数
//升序
oderBy("要排序的字段名",'asc')
//降序
oderBy("要排序的字段名",'desc')
//按商品价格进行排序
order(){
wx.cloud.database().collection('goods')
.orderBy("price",'asc')
.get()
.then(res=>{
console.log('商品列表请求成功',res)
this.setData({
list:res.data
})
})
.catch(err=>{
console.error('商品列表请求失败',err)
})
}
3.修改上次遗留的bug,修改数据类型
上次我们通过input函数添加数据的商品名和价格都是字符串类型,要进行商品价格的排序必须全部统一成数值类型或者浮点型