HarmonyOS 鸿蒙Next ForEach中item的数字如何做累加,代码如下

发布于 1周前 作者 zlyuanteng 来自 鸿蒙OS

HarmonyOS 鸿蒙Next ForEach中item的数字如何做累加,代码如下

请教各位,代码如下,有什么办法可以把三个计数器中的数字累加起来~

@Entry
@Component
struct Index {
  private num: number = 0

  build() {
    Column({ space: 20 }) {

      ForEach(DATA, (item: Goods) => {
        Comp({ str: item.goodsCount })
      })

      Text(`总数: ${this.num}`).fontSize(25)

    }.width('100%')
  }
}

@Component
struct Comp {
  private str: number = 0
  @State num: number = 0

  build() {
    Row({ space: 20 }) {
      Text(this.str.toString())
      Row() {
        Button('-').type(ButtonType.Normal).onClick(() => {
          this.num--
        })

        Text(this.num.toString())

        Button('+').type(ButtonType.Normal).onClick(() => {
          this.num++
        })

      }
    }
  }
}

export class Goods {
  goodsCount: number = 0 // 总数量
  constructor(goodsCount: number) {
    this.goodsCount = goodsCount
  }
}

const DATA: Goods[] = [
  new Goods(1),
  new Goods(2),
  new Goods(3)
]

更多关于HarmonyOS 鸿蒙Next ForEach中item的数字如何做累加,代码如下的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html

7 回复
@Entry
@Component
struct Index {
  @State num: number = 0

  aboutToAppear(): void {
    let tempTotal = 0
    for (let i = 0; i < DATA.length; i++) {
      tempTotal += DATA[i].goodsCount
    }
    this.num = tempTotal
  }

  build() {
    Column({ space: 20 }) {
      ForEach(DATA, (item: Goods, index: number) {
        Comp({
          str: item.goodsCount,
          item: item,
          onChange: (value: string) => {
            DATA[index] = JSON.parse(value) as Goods
            this.aboutToAppear()
          }
        })
      })

      Text(`总数: ${this.num}`).fontSize(25)

    }.width('100%')
  }
}

@Component
struct Comp {
  @Prop item: Goods
  private str: number = 0
  onChange: Function = () => {
  }
 
  build() {
    Row({ space: 20 }) {
      Text(this.str.toString())
      Row() {
        Button('-').type(ButtonType.Normal).onClick(() => {
          this.item.goodsCount--
          this.onChange(JSON.stringify(this.item))
        })
        Text(this.item.goodsCount.toString())
        Button('+').type(ButtonType.Normal).onClick(() => {
          this.item.goodsCount++
          this.onChange(JSON.stringify(this.item))
        })

      }
    }
  }
}

export class Goods {
  goodsCount: number = 0 // 总数量
  constructor(goodsCount: number) {
    this.goodsCount = goodsCount
  }
}

const DATA: Goods[] = [
  new Goods(1),
  new Goods(2),
  new Goods(3)
]

更多关于HarmonyOS 鸿蒙Next ForEach中item的数字如何做累加,代码如下的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


这个更优雅~

找HarmonyOS工作还需要会Flutter的哦,有需要Flutter教程的可以学学大地老师的教程,很不错,B站免费学的哦:BV1S4411E7LY/?p=17

方案三,在类中添加一个变量纪录总数

@Entry
@Component
struct Index {
  @State num: number = 0
  private goods: Array<Goods> = DATA;

  onCountChange() {
    let localTotalMoney = 0;
    this.goods.forEach((item, index) => {
      localTotalMoney += item.goodsCount
    })
    this.num = localTotalMoney
  }

  build() {
    Column({ space: 20 }) {

      ForEach(this.goods, (item: Goods) => {
        Comp({ str: item.Count, onCountChange: (newCount: number) => {
          item.goodsCount = newCount
          this.onCountChange()
        } })
      })

      Text(`总数: ${this.num}`).fontSize(25)

    }.width('100%')
  }
}

@Component
struct Comp {
  private str: number = 0
  @State num: number = 0
  private onCountChange: (newCount: number) => void;

  build() {
    Row({ space: 20 }) {
      Text(this.str.toString())
      Row() {
        Button('-').type(ButtonType.Normal).onClick(() => {
          this.num--
          this.onCountChange(this.num)
        })

        Text(this.num.toString())

        Button('+').type(ButtonType.Normal).onClick(() => {
          this.num++
          this.onCountChange(this.num)
        })

      }
    }
  }
}

export class Goods {
  Count: number; //数量
  goodsCount: number = 0 //总数量
  constructor(Count: number) {
    this.Count = Count
  }
}

const DATA: Goods[] = [
  new Goods(1),
  new Goods(2),
  new Goods(3)
]

方案二

@Entry
@Component
struct Index {
  @State DATA: Goods[] = [
    new Goods(),
    new Goods(),
    new Goods()
  ]
  @State num: number = 0

  onGoodsCountChange(index: number, delta: number): void {
    this.DATA[index].goodsCount += delta
    this.DATA[index] = JSON.parse(JSON.stringify(this.DATA[index]))
    let tempTotal = 0
    for (let i = 0; i < this.DATA.length; i++) {
      tempTotal += this.DATA[i].goodsCount
    }
    this.num = tempTotal
  }

  build() {
    Column({ space: 20 }) {
      ForEach(this.DATA, (item: Goods, index: number) => {
        Comp({
          index: index,
          item: item,
          onChange: (delta: number) => this.onGoodsCountChange(index, delta)
        })
      })
      Text(`总数: ${this.num}`).fontSize(25)
    }.width('100%')
  }
}

@Component
struct Comp {
  @Prop item: Goods
  private index: number = 0
  onChange: Function = () => {
  }

  build() {
    Row({ space: 20 }) {
      Text((this.index + 1).toString())
      Row() {
        Button('-').type(ButtonType.Normal).onClick(() => {
          this.onChange(-1)
        })
        Text(this.item.goodsCount.toString())
        Button('+').type(ButtonType.Normal).onClick(() => {
          this.onChange(1)
        })
      }
    }
  }
}

export class Goods {
  goodsCount: number = 0 // 总数量
}

在HarmonyOS中,ForEach用于遍历数组并生成UI组件。要在ForEach中对item的数字进行累加,可以通过在ForEach外部定义一个变量来存储累加结果,并在遍历过程中更新该变量。以下是一个示例代码:

@Entry
@Component
struct MyComponent {
  @State numbers: number[] = [1, 2, 3, 4, 5];
  @State sum: number = 0;

  build() {
    Column() {
      ForEach(this.numbers, (item: number) => {
        this.sum += item;
        Text(`Item: ${item}, Sum: ${this.sum}`)
      })
    }
  }
}

在这个示例中,sum变量用于存储累加结果。每次遍历时,sum会加上当前的item值,并显示在Text组件中。注意,sum需要在@State中声明,以便在UI更新时保持状态。

在HarmonyOS的ForEach中,你可以通过定义一个累加变量来实现数字的累加。以下是一个示例代码:

@State numbers: number[] = [1, 2, 3, 4, 5];
@State sum: number = 0;

build() {
  Column() {
    ForEach(this.numbers, (item: number) => {
      this.sum += item;
      Text(`Item: ${item}, Sum: ${this.sum}`)
    })
  }
}

在这个例子中,sum变量会在每次遍历时累加item的值,并显示当前累加结果。

回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!