当前位置:   article > 正文

ArkTS 循环控制_arkts 列表循环

arkts 列表循环

 在ArkTS开发中,我们使用ForEach / if-else 来控制页面的循环布局。

 ForEach

ForEach(

arr:Array, //数组

(item:any,index?:number)=>{

        Row(){

            ...

        }

},

keyGenerator?:(item: any,index?: number): string => {。  // 非常重要 大多数情况下不传

       // 键生成函数 为数组每一项生成一个唯一标识,作为组件是否重新渲染的判断标准。

        }

)

使用foreach 循环渲染一个商品页

这里我们构建一个类,写出一个数组来,将数组作为参数穿入下面的foreach中,完成循环渲染出一个商品列表。 

  1. class Item {
  2. name: string
  3. image: ResourceStr
  4. price: number
  5. constructor(name: string, image: ResourceStr, price: number){
  6. this.name = name
  7. this.image = image
  8. this.price = price
  9. }
  10. }
  11. @Entry
  12. @Component
  13. struct Index {
  14. private items: Array<Item> = [
  15. new Item('os1',$r('app.media.hongmeng'),2000),
  16. new Item('os2',$r('app.media.hongmeng'),3000),
  17. new Item('os3',$r('app.media.hongmeng'),4000),
  18. new Item('os4',$r('app.media.hongmeng'),5000),
  19. new Item('os5',$r('app.media.hongmeng'),6000)
  20. ]
  21. build() {
  22. Column({space:8}){
  23. Row(){
  24. Text('商品列表')
  25. .fontSize(30)
  26. .fontWeight(FontWeight.Bold)
  27. }
  28. .width('100%')
  29. .margin({bottom: 20})
  30. // 循环控制
  31. ForEach(
  32. this.items,
  33. item=>{
  34. Row({space:10}){
  35. Image(item.image)
  36. .width(100)
  37. Column({space: 4}){
  38. Text(item.name)
  39. .fontSize(20)
  40. .fontWeight(FontWeight.Bold)
  41. Text('¥' + item.price)
  42. .fontSize(18)
  43. .fontColor('#ff0000')
  44. }
  45. .height('100%')
  46. .alignItems(HorizontalAlign.Start)
  47. }
  48. .width('100%')
  49. .backgroundColor('#fff')
  50. .borderRadius(20)
  51. .height(120)
  52. .padding(10)
  53. }
  54. )
  55. }
  56. .width('100%')
  57. .height('100%')
  58. .backgroundColor('#efefef')
  59. .padding(20)
  60. }
  61. }

 if-else

if(判断条件){

 //内容

}else{

//内容

}

通过判断条件决定使用哪种方式渲染

 通过if-else来完成折扣价格的实现

  1. class Item {
  2. name: string
  3. image: ResourceStr
  4. price: number
  5. discount: number
  6. constructor(name: string, image: ResourceStr, price: number,discount: number = 0){
  7. this.name = name
  8. this.image = image
  9. this.price = price
  10. this.discount = discount
  11. }
  12. }
  13. @Entry
  14. @Component
  15. struct Index {
  16. private items: Array<Item> = [
  17. new Item('os1',$r('app.media.hongmeng'),2000,500),
  18. new Item('os2',$r('app.media.hongmeng'),3000),
  19. new Item('os3',$r('app.media.hongmeng'),4000,300),
  20. new Item('os4',$r('app.media.hongmeng'),5000),
  21. new Item('os5',$r('app.media.hongmeng'),6000)
  22. ]
  23. build() {
  24. Column({space:8}){
  25. Row(){
  26. Text('商品列表')
  27. .fontSize(30)
  28. .fontWeight(FontWeight.Bold)
  29. }
  30. .width('100%')
  31. .margin({bottom: 20})
  32. // 循环控制
  33. ForEach(
  34. this.items,
  35. item=>{
  36. Row({space:10}){
  37. Image(item.image)
  38. .width(100)
  39. Column({space: 4}){
  40. if(item.discount){
  41. // 名字
  42. Text(item.name)
  43. .fontSize(20)
  44. .fontWeight(FontWeight.Bold)
  45. // 原价
  46. Text('原价: ¥ ' + item.price)
  47. .fontSize(16)
  48. .fontColor('#ccc')
  49. // 装饰效果
  50. .decoration({type:TextDecorationType.LineThrough})
  51. // 折扣价
  52. Text('折扣价: ¥ '+(item.price - item.discount))
  53. .fontSize(18)
  54. .fontColor('#ff0000')
  55. // 补贴信息
  56. Text('补贴: ¥ ' + item.discount)
  57. .fontSize(18)
  58. .fontColor('#ff0000')
  59. }else{
  60. Text(item.name)
  61. .fontSize(20)
  62. .fontWeight(FontWeight.Bold)
  63. Text('¥' + item.price)
  64. .fontSize(18)
  65. .fontColor('#ff0000')
  66. }
  67. }
  68. .height('100%')
  69. .alignItems(HorizontalAlign.Start)
  70. }
  71. .width('100%')
  72. .backgroundColor('#fff')
  73. .borderRadius(20)
  74. .height(120)
  75. .padding(10)
  76. }
  77. )
  78. }
  79. .width('100%')
  80. .height('100%')
  81. .backgroundColor('#efefef')
  82. .padding(20)
  83. }
  84. }

 

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

闽ICP备14008679号