当前位置:   article > 正文

鸿蒙 - arkTs:渲染(循环 - ForEach,判断 - if)_鸿蒙系统循环渲染

鸿蒙系统循环渲染

ForEach循环渲染:

参数:

  1. 要循环遍历的数组,Array类型
  2. 遍历的回调方法,Function类型
  3. 为每一项生成唯一标识符的方法,有默认生成方法,非必传

使用示例: 

  1. interface Item {
  2. name: String,
  3. price: Number
  4. }
  5. @Entry
  6. @Component
  7. struct Index {
  8. private arr:Array<Item> = [
  9. {name: '华为 Meta 50', price: 6999},
  10. {name: '华为 Meta 60 pro', price: 7999},
  11. {name: '华为 Meta X5', price: 12999},
  12. ];
  13. build() {
  14. Column({space: 30}) {
  15. ForEach(
  16. this.arr,
  17. item=>{
  18. Row(){
  19. Column() {
  20. Text(item.name)
  21. .fontWeight(FontWeight.Bold)
  22. .margin({bottom: 10})
  23. Text('¥' + item.price)
  24. }
  25. }
  26. .width('100%')
  27. .backgroundColor("#FFF")
  28. .padding(20)
  29. }
  30. )
  31. }
  32. .height('100%')
  33. .backgroundColor("#999")
  34. .justifyContent(FlexAlign.Center)
  35. };
  36. }

效果展示:


if判断渲染:

使用示例:

  1. interface Item {
  2. name: String,
  3. price: Number,
  4. discount?: Number
  5. }
  6. @Entry
  7. @Component
  8. struct Index {
  9. private arr:Array<Item> = [
  10. {name: '华为 Meta 50', price: 6999, discount: 6666},
  11. {name: '华为 Meta 60 pro', price: 7999},
  12. {name: '华为 Meta X5', price: 12999},
  13. ];
  14. build() {
  15. Column({space: 30}) {
  16. ForEach(
  17. this.arr,
  18. item=>{
  19. Row(){
  20. Column() {
  21. if(item.discount){
  22. Text(item.name)
  23. .fontWeight(FontWeight.Bold)
  24. .margin({bottom: 10})
  25. Text('原价:¥' + item.price)
  26. .fontSize(14)
  27. .decoration({ type: TextDecorationType.LineThrough })
  28. Text('折扣价:¥' + item.discount)
  29. .textAlign(TextAlign.Start)
  30. Text('补贴:¥' + (item.price - item.discount))
  31. .textAlign(TextAlign.Start)
  32. }else{
  33. Text(item.name)
  34. .fontWeight(FontWeight.Bold)
  35. .margin({bottom: 10})
  36. Text('¥' + item.price)
  37. }
  38. }
  39. }
  40. .width('100%')
  41. .backgroundColor("#FFF")
  42. .padding(20)
  43. }
  44. )
  45. }
  46. .height('100%')
  47. .backgroundColor("#999")
  48. .justifyContent(FlexAlign.Center)
  49. };
  50. }

效果展示:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/249637
推荐阅读
相关标签
  

闽ICP备14008679号