前言
- 定义两个按钮点击切换(
v-show
) - 利用sort()方法排序。
使用
- 定义两个按钮
<el-button v-show="!flag" @click="xun">⬆</el-button>
<el-button v-show="flag" @click="xun">⬇</el-button>
- data中的数据
data(){
return{
flag:false,
}
}
- 定义升序降序的方法
xun() {
this.flag = !this.flag;
if (this.flag) {
//升序
this.cart.sort((a, b) => {
return a.price - b.price;
});
} else {
//降序
this.cart.sort((a, b) => {
return b.price - a.price;
});
}
}
//this.cart为获取的数据
总结
初出茅庐,请各位大佬多多指教。