HarmonyOS Next星河版 ArkUI渲染控制

一、ForEach

语法:

private items = [
    {
        name:"admin",
        address:"郑州"
    },
    {
        name:"user",
        address:"云南"  
    },
    {
        name:"super",
        address:"江西"  
    }
]


ForEach(
    array:Array, // 要遍历的数据数组
    (item:any,index?:number)=>{ // 遍历
        
    },
    // 键生成函数,为数组每一项生成一个唯一标识,组件是否重新渲染的判断标准
    // 类似于Vue中for循环的key,这里不需要定义key,默认有值
    keyGenerator?:(item:any,index?:number):string=>{
    
    }
)
class Shop{
  name:string
  img:ResourceStr
  price:number

  constructor(name:string,img:ResourceStr,price:number) {
    this.name = name
    this.img = img
    this.price = price
  }
}

@Entry
@Component
struct ForEachUI {

  private shops:Array<Shop> = [
    new Shop("华为mate70旗舰手机",$r("app.media.down"),8999),
    new Shop("华为mate70旗舰手机",$r("app.media.down"),9999),
    new Shop("华为mate70旗舰手机",$r("app.media.down"),10999)
  ]

  build() {
    Column(){
      Row(){
        Text("商品列表")
          .fontSize(30)
      }
      .width("90%")
      .height("60")
      
      ForEach(this.shops,(item:Shop) =>{
        Row(){
          Image(item.img)
            .width(100)

          Column(){
            Text(item.name)
              .fontWeight(FontWeight.Bold)
            Text("¥"+item.price)
              .fontColor("red")
          }
          .alignItems(HorizontalAlign.Start)
        }
        .width("90%")
        .backgroundColor("#ffffff")
        .height("15%")
        .borderRadius(8)
        .margin({bottom:10})
      })
      
      
      


    }
    .width("100%")
    .height("100%")
    .backgroundColor("#c7c7c7")
  }
}

在这里插入图片描述

二、if-else

语法:

// 在TS中,如果获取的值为0,那么条件结果为false,否则为true
if(0:1){

}else{

}

三、需求

在这里插入图片描述

class Shop{
  name:string
  img:ResourceStr
  price:number
  status:number

  constructor(name:string,img:ResourceStr,price:number,status:number) {
    this.name = name
    this.img = img
    this.price = price
    this.status = status
  }
}

@Entry
@Component
struct ForEachUI {

  private shops:Array<Shop> = [
    new Shop("华为mate70旗舰手机",$r("app.media.down"),8999,500),
    new Shop("华为mate70旗舰手机",$r("app.media.down"),9999,200),
    new Shop("华为mate70旗舰手机",$r("app.media.down"),10999,0)
  ]

  build() {
    Column(){
      Row(){
        Text("商品列表")
          .fontSize(30)
      }
      .width("90%")
      .height("60")
      
      ForEach(this.shops,(item:Shop) =>{
        Row(){
          Image(item.img)
            .width(100)

          Column({space:4}){

            if (item.status) {
              Text(item.name)
                .fontWeight(FontWeight.Bold)
              Text("原价:¥"+item.price)
                .fontColor("#c7c7c7")
                .decoration({type:TextDecorationType.LineThrough})
                .fontSize(14)
              Text("折扣价:¥"+(item.price - item.status))
                .fontColor("red")
              Text("补贴:¥"+item.status)
                .fontColor("red")
            } else{
              Text(item.name)
                .fontWeight(FontWeight.Bold)
              Text("¥"+item.price)
                .fontColor("red")
            }
            
            
            

          }
          .alignItems(HorizontalAlign.Start)
        }
        .width("90%")
        .backgroundColor("#ffffff")
        .height("15%")
        .borderRadius(8)
        .margin({bottom:10})
      })
      
      
      


    }
    .width("100%")
    .height("100%")
    .backgroundColor("#c7c7c7")
  }
}
eight("15%")
        .borderRadius(8)
        .margin({bottom:10})
      })
      
      
      


    }
    .width("100%")
    .height("100%")
    .backgroundColor("#c7c7c7")
  }
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刘程云

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值