HarmonyOS 数据结构多级嵌套如何局部刷新

List里面嵌套ListItemGroup, ListItemGroup里面嵌套ListItem

我们的数据结构上最外层上一个数组,数组元素上一个对象,对象里面又是对象的数组,类似这样的,我们想修改一个属性就刷新界面,点击按钮数据更新了,但是ui没有刷新

HarmonyOS
2024-12-25 07:56:42
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Heiang

1、参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-observed-and-objectlink-V5#%E5%9F%BA%E7%A1%80%E5%B5%8C%E5%A5%97%E5%AF%B9%E8%B1%A1%E5%B1%9E%E6%80%A7%E6%9B%B4%E6%94%B9%E5%A4%B1%E6%95%88

2、参考代码:

@Entry
@Component
struct Home123 {
  @State modelAList: ModelAArray = [new ModelA(), new ModelA(), new ModelA()]

  build() {
    Column() {
      RecordPage({ modelAList: this.modelAList })
    }.width('100%').height('100%')
  }
}
@Component
struct RecordPage {
  @State message: string = 'Hello World';
  @ObjectLink modelAList: ModelAArray;
  build() {
    Column() {
      Button("全选/全不选")
        .onClick(() => {
          this.modelAList.forEach((modelA: ModelA) => {
            modelA.modelBList.forEach((modelB: ModelB) => {
              modelB.modelC.isSelected = !modelB.modelC.isSelected;
              modelB.modelC.goodsImage = modelB.modelC.goodsImage === '商品1' ? '商品2' : '商品1';
            })
          })
        })
      List() {
        ForEach(this.modelAList, (modelA: ModelA, index: number) => {
          GroupItemView({
            modelA: modelA,
            index: index
          })
        })
      }

    }
    .width("100%")
    .height("100%")

  }
}
@Component
struct GroupItemView {
  @ObjectLink modelA: ModelA;
  index: number = 0
  @Builder
  GroupTitle(index: number) {
    Text("分组" + index)
  }
  build() {
    ListItemGroup({ header: this.GroupTitle(this.index) }) {
      ForEach(this.modelA.modelBList, (modelB: ModelB) => {
        ChildItem({
          modelB: modelB
        })
      })
    }
  }
}
@Component
struct ChildItem {
  @ObjectLink modelB: ModelB
  build() {
    ListItem() {
      Row() {
        ChildItemInside({ modelC: this.modelB.modelC })
      }
      .height(100)
      .width("100%")
    }
  }
}
@Component
struct ChildItemInside {
  @ObjectLink modelC: ModelC;
  build() {
    Row({ space: 20 }) {
      Text(this.modelC.goodsImage).fontSize(20)
      Checkbox()
        .select(this.modelC.isSelected)
    }
  }
}
@Observed
class ModelA {
  name: string = ""
  age: number = 0;
  modelBList: ModelBArray = [new ModelB(1, "篮球"), new ModelB(1, "足球"), new ModelB(1, "乒乓球")]
}
@Observed
class ModelAArray extends Array<ModelA> {
}
@Observed
class ModelB {
  sex: number = 0
  hobby: string = ""
  modelC: ModelC = new ModelC("商品1", true)
  constructor(sex: number, hobby: string) {
    this.sex = sex;
    this.hobby = hobby;
  }
}
@Observed
class ModelC {
  goodsImage?: string //商品图片url
  isSelected: boolean = true;
  constructor(goodImage: string, isSelected: boolean) {
    this.goodsImage = goodImage;
    this.isSelected = isSelected;
  }
}
@Observed
class ModelBArray extends Array<ModelB> {
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
分享
微博
QQ
微信
回复
2024-12-25 10:45:10
相关问题
HarmonyOS 数据结构咨询
434浏览 • 1回复 待解决
HarmonyOS ForEach局部刷新
1022浏览 • 1回复 待解决
数据结构与算法分析习题4.33和4.34
4384浏览 • 1回复 待解决
HarmonyOS list局部刷新的问题
230浏览 • 1回复 待解决
HarmonyOS中List组件是否支持局部刷新
911浏览 • 1回复 待解决
Swiper 组件嵌套图片刷新数据会闪烁
1777浏览 • 1回复 待解决