HarmonyOS 数组元素删除之后 未进行页面刷新

如图所示

ZLKTshowList 是一个数组 使用State修饰的 里面存放的是对象

listModel 是一个实例对象,里面有一个数组showList

我进行数组删除时 删除showList里面的元素 页面没有进行刷新

HarmonyOS  数组元素删除之后 未进行页面刷新  -鸿蒙开发者社区

HarmonyOS
2024-12-24 14:58:57
826浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

请参考如下代码:

@Entry
@Component
struct ListItemGroupExample {
  @State timeTable: TimeTableModel[] = [

    new TimeTableModel('星期一', false,
      [new TimeTableChildModel('物理', false), new TimeTableChildModel('ds物理ds', true)]),

    new TimeTableModel('星期二', false,
      [new TimeTableChildModel('物理', false)]),

  ]

  @Builder
  itemHead(item: TimeTableModel, index: number) {
    aaa({
      item: item,
      index: index,
      headerClick: (itemd, indexd) => {
        // this.timeTable.splice(indexd,1,itemd)
      }
    })
  }

  @Builder
  itemFoot(item: TimeTableModel) {
    ccc({
      item: item
    })
    // Text('共' + num + "节课")
    //   .fontSize(16)
    //   .backgroundColor(0xAABBCC)
    //   .width("100%")
    //   .padding(5)
  }

  build() {
    Column() {
      List({ space: 20 }) {
        ForEach(this.timeTable, (item: TimeTableModel, index
          : number) => {
          ListItemGroup({
            header: this.itemHead(item, index),
            footer: this.itemFoot(item)
          }) {
            ViewPage({
              item: item
            })
            // ForEach(item.projects, (project: TimeTableChildModel) => {
            //   ListItem() {
            //     Text(project.title)
            //       .width("100%")
            //       .height(100)
            //       .fontSize(20)
            //       .textAlign(TextAlign.Center)
            //       .backgroundColor(0xFFFFFF)
            //   }
            // })
          }

        })
      }
      .width('90%')
      .scrollBar(BarState.Off)
    }.width('100%').height('100%').backgroundColor(0xDCDCDC).padding({ top: 5 })
  }
}

@Component
struct ViewPage {
  @ObjectLink item: TimeTableModel
  build() {
    Column() {
      ForEach(this.item.projects, (project: TimeTableChildModel,index:number) => {
        ListItem() {
          ddd({ item: project, index:index })
          // Text(project.title)
          //   .width("100%")
          //   .height(100)
          //   .fontSize(20)
          //   .textAlign(TextAlign.Center)
          //   .backgroundColor(0xFFFFFF)
          //   .onClick(()=>{
          //     project.title = "ddd"
          //   })
        }
      })
    }

  }
}

@Observed
class TimeTableModel {
  title: string = '';
  isClose: boolean = true
  projects: TimeTableChildModel[];

  constructor(title: string, isClose: boolean, projects: TimeTableChildModel[]) {
    this.title = title;
    this.isClose = isClose;
    this.projects = projects;
  }
}

@Observed
class TimeTableChildModel {
  title: string = '';
  isPlaying: boolean = true
  constructor(title: string, isPlaying: boolean) {
    this.title = title;
    this.isPlaying = isPlaying;
  }
}

@Component
struct aaa {
  @ObjectLink item: TimeTableModel
  private index: number = 0
  headerClick: (model: TimeTableModel, index: number) => void = () => {
  }

  build() {
    Text(this.item.title)
      .fontSize(20)
      .backgroundColor(0xAABBCC)
      .width("100%")
      .padding(10)
      .onClick(() => {
        // this.item.projects[0] = 'dsafsdf'
        // this.item.projects.length = 0
        // this.item.projects.pop()
        // this.item.title = 'dasfassdaf'
        this.item.isClose = !this.item.isClose
        // if (this.item.isClose) {
        // this.item.projects = [new TimeTableChildModel('物理21', false)]
        // this.item.projects.push(new TimeTableChildModel('物理21', false))
        // } else {
        this.item.projects = []

      })
  }
}

@Component
struct ccc {
  @ObjectLink item: TimeTableModel
  build() {
    Text('共' + this.item.projects.length + "节课")
      .fontSize(16)
      .backgroundColor(0xAABBCC)
      .width("100%")
      .padding(5)
  }
}

@Component
struct ddd {
  @ObjectLink item: TimeTableChildModel
  @Prop index:number
  build() {
    Column() {
      Text(this.item.title)
        .width("100%")
        .height(100)
        .fontSize(20)
        .textAlign(TextAlign.Center)
        .backgroundColor(0xFFFFFF)
        .onClick(() => {
          this.item.title = "ddd"
        })
        .border({
          width:{top:this.index==0?0:1},
          color:Color.Blue,
        })
    }
  }
}
  • 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.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
分享
微博
QQ
微信
回复
2024-12-24 17:52:22


相关问题
数组元素变更如何触发刷新list?
860浏览 • 1回复 待解决
HarmonyOS 嵌套数组元素的UI刷新方案
1018浏览 • 1回复 待解决
HarmonyOS NavPathStack如何删除元素
695浏览 • 1回复 待解决
readonly修饰的数组无法获取数组元素
3084浏览 • 1回复 待解决
HarmonyOS 数组对象数据刷新
838浏览 • 1回复 待解决
Record<string, string>如何删除里边的元素
2240浏览 • 1回复 待解决
如何删除数组中的空值?
1075浏览 • 1回复 待解决
HarmonyOS 二维数组刷新问题
1059浏览 • 1回复 待解决
class二次刷新渲染数组
1556浏览 • 1回复 待解决
ForEach数组发生改变。UI没刷新
1667浏览 • 1回复 待解决