鸿蒙购物车布局完成后的点击事件(不怎么普通易懂) ...

打印 上一主题 下一主题

主题 1798|帖子 1798|积分 5394

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. import router from '@ohos.router';
  2. import { ShopCarEntity } from '../../entity/ShopCarEntity';
  3. import LocDataModel from '../../viewmodel/LocDataModel';
  4. import data from '@ohos.telephony.data';
  5. @Entry
  6. @Component
  7. export default struct CarPage {
  8.   @State carList:ShopCarEntity[]=LocDataModel.DEFAULT_SHOP_CART_LIST
  9.   @State totalPrice :number =0
  10.   @State totalCount:number =0
  11.   //计算总价和数量
  12.   countTotalNumber(){
  13.     let tempTotalPrice=0
  14.     let temTotalCount=0
  15.     this.carList.forEach((item:ShopCarEntity)=>{
  16.       if (item.isSelected) {
  17.         tempTotalPrice+=item.goods_default_price*item.count
  18.         temTotalCount+=item.count
  19.       }
  20.     })
  21.     this.totalPrice=tempTotalPrice
  22.     this.totalCount=temTotalCount
  23.   }
  24.   //处理商品单选操作
  25.   carItemSelectChanged(state:boolean,index:number){
  26.     let newCarlist:ShopCarEntity[]=[]
  27.     this.carList.forEach((item:ShopCarEntity,itemIndex:number)=>{
  28.       if (itemIndex===index) {
  29.         item.isSelected=state
  30.         item.itemkey=`${Math.random()*10}-${Math.random()}`
  31.       }
  32.       newCarlist.push(item)
  33.     })
  34.     this.carList=newCarlist
  35.   }
  36.   build() {
  37.      Flex({direction:FlexDirection.Column}){
  38.         //标题部分
  39.         this.TitleBarComponent()
  40.        //商品列表
  41.        this.CarListCompoent()
  42.        //底部
  43.        this.BottomOptionsCompoent()
  44.      }
  45.     .width('100%')
  46.     .height('100%')
  47.     .backgroundColor($r('app.color.page_background'))
  48.   }
  49.   /**
  50.    * 标题
  51.    */
  52.   @Builder TitleBarComponent(){
  53.     Stack({alignContent:Alignment.End}){
  54.       Text('购物车')
  55.         .width('100%')
  56.         .textAlign(TextAlign.Center)
  57.         .fontColor(Color.White)
  58.         .fontSize(16)
  59.         .fontWeight(FontWeight.Bold)
  60.       Text('删除已选')
  61.         .borderWidth(1).
  62.       borderColor(Color.White).
  63.       borderRadius(12).
  64.       fontColor(Color.White).
  65.       fontSize(12).
  66.       fontWeight(FontWeight.Bold)
  67.         .padding({
  68.           top:5,
  69.           right:8,
  70.           bottom:5,
  71.           left:8
  72.         })
  73.         .textAlign(TextAlign.Center)
  74.         .lineHeight(12)
  75.         .margin({
  76.           right:15
  77.         })
  78.     }.width('100%')
  79.     .height('56vp')
  80.     .backgroundColor('#ff0000')
  81.   }
  82.   /**
  83.    * 添加商品数量
  84.    *
  85.    */
  86.   addGoodsCount(index:number){
  87.     let newCarList:ShopCarEntity[]=[]
  88.     this.carList.forEach((item:ShopCarEntity,itemIndex:number)=>{
  89.       if (itemIndex===index) {
  90.         item.count++
  91.         item.itemkey=`${Math.random()*10}-${item.id}-${Math.random()}}`
  92.       }
  93.       newCarList.push(item)
  94.     })
  95.     //重新赋值
  96.     this.carList=newCarList
  97.   }
  98.   /**
  99.    * 减少商品数量
  100.    */
  101.   reduceGoodsCount(index:number){
  102.     let newCarList:ShopCarEntity[]=[]
  103.     this.carList.forEach((item:ShopCarEntity,itemIndex:number)=>{
  104.       if (itemIndex===index) {
  105.         item.count--
  106.         item.itemkey=`${Math.random()*10}-${item.id}-${Math.random()}`
  107.       }
  108.       newCarList.push(item)
  109.     })
  110.     //重新赋值
  111.     this.carList=newCarList
  112.   }
  113.   /**
  114.    * 给itemView生成唯一的key
  115.    *
  116.    */
  117.   generatorItemViewKey(data:ShopCarEntity):string{
  118.     if (data.itemkey) {
  119.       return data.itemkey
  120.     }
  121.     return `${Math.random()*10}*${data.id}--${Math.random()}]`
  122.   }
  123.   /**
  124.    * 商品列表
  125.    */
  126.   @Builder CarListCompoent(){
  127.     Scroll(){
  128.       Column(){
  129.         if (this.carList.length>0){
  130.            List({space:2}){
  131.              ForEach(this.carList,(item:ShopCarEntity,index:number)=>{
  132.                ListItem(){
  133.                  this.CarItemComponent(item,index)
  134.                }
  135.              },(item:ShopCarEntity) => this.generatorItemViewKey(item))
  136.            }
  137.         }else {
  138.           this.EmptyComponent()
  139.         }
  140.       }.width('100%').height('100%')
  141.     }.width('100%')
  142.     .flexGrow(1)
  143.   }
  144.   @Builder
  145.   CarItemComponent(data:ShopCarEntity,index:number){
  146.     Flex({direction:FlexDirection.Row,alignItems:ItemAlign.Center}){
  147.       //复选框
  148.       Checkbox().width(20).height(20).select(true).selectedColor($r('app.color.focus_color'))
  149.         .onChange((value:boolean)=>{
  150.             this.carItemSelectChanged(value,index)
  151.         })
  152.       Image(data.goods_default_icon)
  153.         .margin({left:10,right:10})
  154.         .width(100).height(100).objectFit(ImageFit.Contain).key(data.goods_id.toString())
  155.       //商品描述和价格
  156.       Column(){
  157.         Text(data.goods_desc).fontSize(14).fontColor(Color.Gray).maxLines(2).textOverflow({overflow:TextOverflow.Ellipsis})
  158.         //商品价格
  159.         Text(){
  160.           Span('价格:').fontSize(12).fontColor(Color.Black)
  161.           Span(`${data.goods_default_price}`).fontColor($r('app.color.focus_color')).fontSize(12)
  162.         }.margin({top:5,bottom:5})
  163.         //数量加减
  164.         Row(){
  165.           Text('数量:').fontSize(12).fontColor(Color.Black)
  166.           Counter(){
  167.             Text(`${data.count}`).fontSize(12)
  168.           }.width(100).height(20)
  169.           .onInc(()=>{
  170.             if (data.count<50) {
  171.               this.addGoodsCount(index)
  172.             }
  173.           })
  174.           .onDec(()=>{
  175.             if (data.count>1) {
  176.               this.reduceGoodsCount(index)
  177.             }
  178.           })
  179.           //空白组件
  180.           Blank().layoutWeight(1)
  181.           //当前商品选中时候,才显示
  182.           if (data.isSelected){
  183.              Image($r('app.media.news_selected')).width(15)
  184.                .objectFit(ImageFit.Contain).onClick(()=>{
  185.              })
  186.           }
  187.         }.alignItems(VerticalAlign.Center)
  188.       }.alignItems(HorizontalAlign.Start)
  189.       .margin({right:5})
  190.     }.padding({left:5,right:5})
  191.     .backgroundColor(Color.White)
  192.   }
  193. @Builder EmptyComponent(){
  194.   Column(){
  195.     Column(){
  196.       Image($r('app.media.caca')).width(70).objectFit(ImageFit.Contain)
  197.       Text('欢迎添加购物车!!!').fontColor(Color.Gray).fontSize(14).margin({
  198.         top:5
  199.       })
  200.     }.justifyContent(FlexAlign.Center)
  201.     .alignItems(HorizontalAlign.Center)
  202.   }.width('100%')
  203.   .height('100%')
  204.   .justifyContent(FlexAlign.Center)
  205. }
  206.   /**
  207.    * 底部操作
  208.    *
  209.    */
  210.   @Builder BottomOptionsCompoent(){
  211.      Flex({direction:FlexDirection.Row,justifyContent:FlexAlign.SpaceBetween,alignItems:ItemAlign.Center}){
  212.        //全选和总价
  213.        Row(){
  214.          Checkbox()
  215.            .width(15)
  216.            .height(15)
  217.            .select(true).selectedColor($r('app.color.select_color'))
  218.            .onChange((value:boolean)=>{
  219.            })
  220.          Text('全选').fontColor(Color.Gray).fontSize(12)
  221.      //总价
  222.          Text(){
  223.            Span('总价: ').fontColor(Color.Black).fontSize(12).fontWeight(FontWeight.Bold)
  224.            Span('qwqqeqeqeq').fontColor($r('app.color.focus_color')).fontSize(12)
  225.            Span('元').fontColor(Color.Black).fontSize(12).fontWeight(FontWeight.Bold)
  226.            Span('12节').fontColor(Color.Black).fontSize(12).fontWeight(FontWeight.Bold)
  227.          }.margin({left:20})
  228.          .textAlign(TextAlign.Start)
  229.        }
  230.        .margin({left:10})
  231.        .height('100%')
  232.        //立即结算
  233.        Button('立即结算').margin({right:10}).width(80).height(35).fontSize(12).backgroundColor($r('app.color.focus_color')).onClick(()=>{
  234.        })
  235.      }.width('100%')
  236.     .height('50vp')
  237.     .backgroundColor(Color.White)
  238.   }
  239. }
  240. 购物车的代码,以及所有的代码点击加减事件结算
复制代码

这是整体的代码,数量加减和多选删除以及结算
欢迎各人一起讨论,修改和增补代码中的不敷之处

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

傲渊山岳

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表