在上一节我们实现了商城的分类功能,实现了商品类别的信息的展示,现在我们的应用已经有两个导航类目可以点击了,现在我们要实现的功能是,当我们点击底部导航购物车选项时,我们要加载购物车相关的内容,以列表的形式展示到页面上
现在我们来看一下我们要实现的样式
购物车的业务逻辑非常的复杂,这里我们先实现一个简单的购物车,然后再对购物车功能进行完善。
首先我们对购物车业务逻辑进行分析
一条数据包含了店铺的信息和店铺是否选中的状态,店铺下方是当前店铺的商品以及选中状态,状态有全选,同时有另一种状态,当我们取消了店铺下商品的选中后,店铺的选中状态也随之取消,初始状态下,商品和店铺都是未选中状态
首先定义好变量,一个商品总价和商品流的详情,因为我们需要改变数组中对象的数据所以我们需要用到@ObservedV2和@Trace装饰器,就可以实现深层次的监听
@ObservedV2
export class Test{
pos:number=0
name:string=’’
@Trace check:boolean=false
@Trace childList:ChileBean[]=[]
}
@ObservedV2
export class ChileBean{
child_pos:number=0
name:string=’’
price:number=0
img:string=’’
@Trace child_check:boolean=false
@Trace num:number=1
maxAmount:number=0
activitiesAmount:number=0
couponPrice:number=0
constructor(child_pos?:number,name?:string,price?:number,couponPrice?:number,img?:string,child_check?: boolean,num?:number,maxAmount?:number,activitiesAmount?:number) {
this.child_pos = child_pos!
this.name = name!
this.price = price!
this.couponPrice= couponPrice!
this.img=this.img!
this.child_check = child_check!
this.num=num!
this.maxAmount=maxAmount!
this.activitiesAmount=activitiesAmount!
}
}
我们创建好数据后,在页面的生命周期中填充数据
aboutToAppear() {
const digitalAppliancesChildren:ChileBean[] = [
new ChileBean(0, “相机”, 1998,1500,‘网络图片地址’,false,1,100,5),
new ChileBean(0, “电视”, 6889,3500,‘网络图片地址’, false,1,10,3),
new ChileBean(0, “手机”, 9999,8888,‘网络图片地址’, false,1,100,-1)
];
// 创建食品生鲜的子项
const freshFoodChildren:ChileBean[] = [
new ChileBean(1, “带鱼”, 27,25,‘网络图片地址’, false,1,10,2),
new ChileBean(1, “螃蟹”, 36,30, ‘网络图片地址’,false,1,10,3),
new ChileBean(1, “大虾”, 19,15,‘网络图片地址’,false,1,20,10)
];
this.arrAy.push(this.createTest(0, “数码电器”, digitalAppliancesChildren));
this.arrAy.push(this.createTest(1, “食品生鲜”, freshFoodChildren));
}
数据准备好后使用list组件进行数据的填充并且加入布局展示数据(代码如下)
List({space:10}) {
ForEach(this.arrAy, (item: Test, index: number) => {
ListItem() {
Column() {
Row() {
Checkbox({ name: ‘checkbox1’ + index, group: ‘checkboxGroup’+index})
.selectedColor("#ffd95112")
.select(item.check)
.shape(CheckBoxShape.CIRCLE)
.onChange((value: boolean) => {
})
.width(20)
.height(20)
Text(item.name)
.fontSize(18)
.fontWeight(FontWeight.Bold)
.fontColor($r(‘app.color.color_red’))
.width(‘100%’)
.onClick(() => {
showToast(item.name)
})
}
List({space:10}) {
ForEach(item.childList, (child: ChileBean, child_index: number) => {
ListItem() {
Row() {
Checkbox({ name: ‘checkbox1’ + child_index, group: ‘checkboxGroup’ + child_index })
.select(child.child_check)
.selectedColor("#ffd95112")
.shape(CheckBoxShape.CIRCLE)
.onChange((value: boolean) => {
})
.width(20)
.height(20)
Image(child.img)
.height(90)
.width(90)
.border({width:1,color:Color.Blue,radius:10})
Column({space:10}){
Text(child.name)
.fontSize(16)
.fontWeight(FontWeight.Bold)
.fontColor($r(‘app.color.tab_text_normal’))
Row(){
Text(){
Span(‘¥’)
.fontSize(14)
.fontWeight(FontWeight.Bold)
.fontColor("#ffe5570b")
Span(""+child.couponPrice)
.fontSize(20)
.fontWeight(FontWeight.Bold)
.fontColor("#ffe5570b")
}
if (child.activitiesAmount>-1){
if (child.num>child.activitiesAmount) {
Text(“x”+child.activitiesAmount)
}
}
}
if (child.activitiesAmount>-1){
if (child.num>child.activitiesAmount) {
Row(){
Text(){
Span(‘¥’)
.fontSize(14)
.fontWeight(FontWeight.Bold)
.fontColor("#ffe5570b")
Span(""+child.price)
.fontSize(20)
.fontWeight(FontWeight.Bold)
.fontColor("#ffe5570b")
}
Text(“x”+Number(child.num-child.activitiesAmount))
}
}
}
}
.margin({left:10})
.alignItems(HorizontalAlign.Start)
Blank()
}
.padding({left:10,right:20})
.alignItems(VerticalAlign.Center)
.width(‘100%’)
.justifyContent(FlexAlign.SpaceBetween)
}
})
}
}
}
})
}
当我们选中商品,或者取消商品的时候,我们需要对价格进行一个展示和计算,下面我们来实现一个自定义的结算模块,结算模块我们需要有一个全选按钮,一个提交按钮,一个商品价格计算的组件,当我们没有商品选择的时候,给提交按钮一个置灰和点击添加商品提示。
Row(){
Checkbox({ name: ‘checkbox_all’ , group: ‘checkboxGroup_all’})
.selectedColor("#ffd95112")
.select(this.isALlCheck())
.shape(CheckBoxShape.CIRCLE)
.width(25)
.height(25)
.onChange((value: boolean) => {
})
Text(“全选”)
.fontSize(16)
Blank()
Text(“提交”)
.border({radius:20})
.width(90)
.textAlign(TextAlign.Center)
.fontColor(Color.White)
.backgroundColor(this.getSelectedChildCount()>0?"#ffe5570b":"#ffc3bdbd")
.height(45)
.fontSize(18)
.fontWeight(FontWeight.Bold)
.onClick(()=>{
showToast(“未选择商品”)
})
.margin({right:10})
}
.width(‘100%’)
.justifyContent(FlexAlign.SpaceBetween)
执行代码后展示如下所示