HarmonyOS 另一个列表改变子属性使列表未能刷新的问题

第二个列表改变第一个列表数据源的子属性使列表刷新。

1.选中或取消列表一的某一项,那一项相应改变选中状态并且将所有选中项展示到列表二中

2.列表二点击删除改变子属性未能刷新列表

@Entry
@Component
struct Index {
  @State userArray: Array<UserBean> = [
    new UserBean('01', '姓名01', false),
    new UserBean('02', '姓名02', false),
    new UserBean('03', '姓名03', false),
    new UserBean('04', '姓名04', false),
    new UserBean('05', '姓名05', false),
    new UserBean('06', '姓名06', false),
  ];
  @State selectedArray: Array<UserBean> = []

  build() {
    Column() {

      Text('列表一')
      Grid() {

        ForEach(this.userArray, (item: UserBean) => {
          GridItem() {
            PassengerItem({
              passenger: item,
              select: item.select,
              callback: () => {
                if (item.select) {
                  this.selectedArray = this.selectedArray.filter((element) => element.userId !== item.userId)
                  item.select = false
                } else {
                  item.select = true
                  this.selectedArray.push(item)
                }
                this.userArray.forEach((cust: UserBean) => {
                  if (item.userId == cust.userId) {
                    cust.select = item.select
                  }
                })
              }
            })
          }
        }, (item: UserBean) => JSON.stringify(item))

      }
      .padding({
        left: 15,
        right: 15,
        top: 2,
        bottom: 10
      })
      .columnsTemplate('1fr 1fr 1fr 1fr')
      .rowsTemplate('1fr 1fr')
      .columnsGap(10)
      .rowsGap(10)
      .height(100)

      Text('列表二')

      ForEach(this.selectedArray, (item: UserBean) => {
        SelectPassengerItem({
          passenger: item,
          callback: () => {
            item.select = false
            this.selectedArray = this.selectedArray.filter((element) => element.userId !== item.userId)
            for (let user of this.userArray) {
              if (user.userId === item.userId) {
                user.select = false
              }
            }
          }
        })
      }, (item: UserBean) => JSON.stringify(item))

    }.width('100%')
    .height('100%')
    .backgroundColor($r('app.color.color_bg'))
  }
}

@Component
export struct PassengerItem {
  @ObjectLink passenger: UserBean
  @Prop select: boolean
  callback: () => void = () => {
  }

  build() {

    Text(this.passenger.name)
      .fontSize(14)
      .fontColor(this.select ? "#1FCB94" : "#333333")
      .border({ width: 1, color: this.select ? "#1FCB94" : "#333333", radius: 5 })
      .textAlign(TextAlign.Center)
      .width('100%')
      .height(40)
      .onClick(() => {
        this.select = !this.select
        this.callback()
      })
  }
}

@Component
export struct SelectPassengerItem {
  @Prop passenger: UserBean
  callback: () => void = () => {
  }

  build() {
    Row() {
      Text('删除').fontSize(14).fontColor("#1FCB94")
        .onClick(() => {
          this.callback()
        })
      Text(this.passenger.name)
        .fontSize(14)
        .fontColor("#1FCB94")
        .textAlign(TextAlign.Center)
        .width('100%')
        .height(40)
    }.padding({ left: 15 })
  }
}

@Observed
export class UserBean {
  // 常旅客id
  userId: string = ''
  // 姓名
  name: string = ''
  // 是否选中
  select: boolean = false

  constructor(userId: string, name: string, select: boolean) {
    this.userId = userId
    this.name = name
    this.select = select
  }
}

HarmonyOS 另一个列表改变子属性使列表未能刷新的问题 -鸿蒙开发者社区

HarmonyOS
5天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
FengTianYa

参考demo如下,其它逻辑请自行实现

@Entry
@Component
struct Index {
  @State userArray: Array<UserBean> = [
    new UserBean('01', '姓名01', false),
    new UserBean('02', '姓名02', false),
    new UserBean('03', '姓名03', false),
    new UserBean('04', '姓名04', false),
    new UserBean('05', '姓名05', false),
    new UserBean('06', '姓名06', false),
  ];
  @State selectedArray: Array<UserBean> = []

  build() {
    Column() {

      Text('列表一')
      Grid() {

        ForEach(this.userArray, (item: UserBean,index:number) => {
          GridItem() {
            PassengerItem({
              passenger: item,
              callback: () => {
                //逻辑请自行实现
                if (item.select) {
                  this.selectedArray.push(item)
                } else {
                  this.selectedArray = this.selectedArray.filter((element) => element.userId !== item.userId)
                }
              }
            })
          }
        }, (item: UserBean) => JSON.stringify(item))

      }
      .padding({
        left: 15,
        right: 15,
        top: 2,
        bottom: 10
      })
      .columnsTemplate('1fr 1fr 1fr 1fr')
      .rowsTemplate('1fr 1fr')
      .columnsGap(10)
      .rowsGap(10)
      .height(100)

      Text('列表二')

      ForEach(this.selectedArray, (item: UserBean,index:number) => {
        SelectPassengerItem({
          passenger: item,
          callback: () => {
            this.selectedArray.splice(index,1)
            for (let user of this.userArray) {
              if (user.userId === item.userId) {
                user.select = false
              }
              console.info('ddddd',user.name,'-',user.select)
            }
          }
        })
      }, (item: UserBean) => JSON.stringify(item))

    }.width('100%')
    .height('100%')
    .backgroundColor(Color.Yellow)
  }
}

@Component
export struct PassengerItem {
  @ObjectLink passenger: UserBean
  callback: () => void = () => {
  }

  build() {

    Text(this.passenger.name)
      .fontSize(14)
      .fontColor(this.passenger.select ? "#1FCB94" : "#333333")
      .border({ width: 1, color: this.passenger.select ? "#1FCB94" : "#333333", radius: 5 })
      .textAlign(TextAlign.Center)
      .width('100%')
      .height(40)
      .onClick(() => {
        this.passenger.select = !this.passenger.select
        this.callback()
      })
  }
}

@Component
export struct SelectPassengerItem {
  @Prop passenger: UserBean
  callback: () => void = () => {
  }

  build() {
    Row() {
      Text('删除').fontSize(14).fontColor("#1FCB94")
        .onClick(() => {
          this.callback()
        })
      Text(this.passenger.name)
        .fontSize(14)
        .fontColor("#1FCB94")
        .textAlign(TextAlign.Center)
        .width('100%')
        .height(40)
    }.padding({ left: 15 })
  }
}

@Observed
export class UserBean {
  // 常旅客id
  userId: string = ''
  // 姓名
  name: string = ''
  // 是否选中
  select: boolean = false

  constructor(userId: string, name: string, select: boolean) {
    this.userId = userId
    this.name = name
    this.select = select
  }
}
分享
微博
QQ
微信
回复
5天前
相关问题
entry中如何拉起另一个Module中Ability
4475浏览 • 1回复 待解决
HarmonyOS 列表刷新问题
852浏览 • 1回复 待解决
HAR能读到另一个HAR里rawfile文件吗
1925浏览 • 1回复 待解决
HarmonyOS ForEach列表刷新问题
317浏览 • 1回复 待解决
编写一个页面,实现不规则列表
891浏览 • 1回复 待解决