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

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

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

HarmonyOS
1天前
浏览
收藏 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> {
}
分享
微博
QQ
微信
回复
23h前
相关问题
HarmonyOS 数据结构咨询
270浏览 • 1回复 待解决
HarmonyOS ForEach局部刷新
523浏览 • 1回复 待解决
数据结构与算法分析习题4.33和4.34
3771浏览 • 1回复 待解决
HarmonyOS中List组件是否支持局部刷新
630浏览 • 1回复 待解决
Swiper 组件嵌套图片刷新数据会闪烁
1267浏览 • 1回复 待解决
List局部刷新,有人知道怎么处理吗?
1295浏览 • 1回复 待解决