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

如图所示

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

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

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

阅读 541
1 个回答

请参考如下代码:

@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,
        })
    }
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进