HarmonyOS TextPicker多列问题?

在使用TextPicker 并且传入数据range类型 string[] 绑定selected 是没问题的

但是 range类型为 TextCascadePickerRangeContent[] 绑定selected绑定不上. 是不支持吗? 还是我写法有问题

private dataList: string[] | TextCascadePickerRangeContent[] = []
private select: number | number[] = []

TextPicker({ range: this.dataList, selected: $$this.select })
  .textStyle({ color: 'rgba(51,51,51,0.5)', font: { size: 14 } })
  .selectedTextStyle({ color: '#333', font: { size: 16, weight: 400 } })
  .width('100%')
  .canLoop(this.canLoop)
  .onChange((value: string | string[], index: number | number[]) => {
    console.info('TextPicker 多列联动:onChange ' + JSON.stringify(value) + ', ' + 'index: ' +
    JSON.stringify(index))
  })
阅读 501
1 个回答

参考如下示例:

@Entry
@Component
struct TextPickerExample {
  private cascade: string[] | TextCascadePickerRangeContent[] = [{
    text: '辽宁省',
    children: [{ text: '沈阳市', children: [{ text: '沈河区' }, { text: '和平区' }, { text: '浑南区' }] },
      { text: '大连市', children: [{ text: '中山区' }, { text: '金州区' }, { text: '长海县' }] }]
  }, {
    text: '吉林省',
    children: [{ text: '长春市', children: [{ text: '南关区' }, { text: '宽城区' }, { text: '朝阳区' }] },
      { text: '四平市', children: [{ text: '铁西区' }, { text: '铁东区' }, { text: '梨树县' }] }]
  }, {
    text: '黑龙江省',
    children: [{ text: '哈尔滨市', children: [{ text: '道里区' }, { text: '道外区' }, { text: '南岗区' }] },
      { text: '牡丹江市', children: [{ text: '东安区' }, { text: '西安区' }, { text: '爱民区' }] }]
  }]
  private select: number | number[] = []

  build() {
    Column() {
      TextPicker({ range: this.cascade, selected: $$this.select }).onChange((value: string | string[], index: number | number[]) => {
        console.info('TextPicker 多列联动:onChange ' + JSON.stringify(value) + ', ' + 'index: ' + JSON.stringify(index))
        console.info('TextPicker 多列联动:select ' + this.select)
      })
    }
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进