HarmonyOS 三级联动

[{"code":"110000","name":"北京市","level":"省级","children":[{"code":"110100","name":"北京市","level":"地级","children":[{"code":"110101","name":"东城区","level":"县级","children":[]},{"code":"110102","name":"西城区","level":"县级","children":[]},{"code":"110105","name":"朝阳区","level":"县级","children":[]},{"code":"110106","name":"丰台区","level":"县级","children":[]},{"code":"110107","name":"石景山区","level":"县级","children":[]},{"code":"110108","name":"海淀区","level":"县级","children":[]},{"code":"110109","name":"门头沟区","level":"县级","children":[]},{"code":"110111","name":"房山区","level":"县级","children":[]},{"code":"110112","name":"通州区","level":"县级","children":[]},{"code":"110113","name":"顺义区","level":"县级","children":[]},{"code":"110114","name":"昌平区","level":"县级","children":[]},{"code":"110115","name":"大兴区","level":"县级","children":[]},{"code":"110116","name":"怀柔区","level":"县级","children":[]},{"code":"110117","name":"平谷区","level":"县级","children":[]},{"code":"110118","name":"密云区","level":"县级","children":[]},{"code":"110119","name":"延庆区","level":"县级","children":[]},{"code":"110100","name":"北京市本级","level":"地级","children":[]}]}]},{"code":"120000","name":"天津市","level":"省级","children":[{"code":"120100","name":"天津市","level":"地级","children":[{"code":"120103","name":"河西区","level":"县级","children":[]},{"code":"120101","name":"和平区","level":"县级","children":[]},{"code":"120102","name":"河东区","level":"县级","children":[]},{"code":"120104","name":"南开区","level":"县级","children":[]},{"code":"120105","name":"河北区","level":"县级","children":[]},{"code":"120106","name":"红桥区","level":"县级","children":[]},{"code":"120110","name":"东丽区","level":"县级","children":[]},{"code":"120111","name":"西青区","level":"县级","children":[]},{"code":"120112","name":"津南区","level":"县级","children":[]},{"code":"120113","name":"北辰区","level":"县级","children":[]},{"code":"120114","name":"武清区","level":"县级","children":[]},{"code":"120115","name":"宝坻区","level":"县级","children":[]},{"code":"120116","name":"滨海新区","level":"县级","children":[]},{"code":"120117","name":"宁河区","level":"县级","children":[]},{"code":"120118","name":"静海区","level":"县级","children":[]},{"code":"120119","name":"蓟州区","level":"县级","children":[]},{"code":"120100","name":"天津市本级","level":"地级","children":[]}]}]},{"code":"130000","name":"河北省","level":"省级","children":[{"code":"130100","name":"石家庄市","level":"地级","children":[{"code":"130102","name":"长安区","level":"县级","children":[]},{"code":"130104","name":"桥西区","level":"县级","children":[]},{"code":"130105","name":"新华区","level":"县级","children":[]},{"code":"130107","name":"井陉矿区","level":"县级","children":[]},{"code":"130108","name":"裕华区","level":"县级","children":[]},{"code":"130109","name":"藁城区","level":"县级","children":[]},{"code":"130110","name":"鹿泉区","level":"县级","children":[]},{"code":"130111","name":"栾城区","level":"县级","children":[]},{"code":"130181","name":"辛集市","level":"县级","children":[]},{"code":"130183","name":"晋州市","level":"县级","children":[]},{"code":"130184","name":"新乐市","level":"县级","children":[]},{"code":"130121","name":"井陉县","level":"县级","children":[]}

使用此数据和使用此类型

 export type Department = {
    code: string;
    name: string;
  };
  export type Region = Department & {
    level: string;
    children?: Array<Region>;
  };

进行自定义封装实现三级联动的效果也是可以的,有样例吗?

  1. 使用TextPicker组件和上面的数据及类型实现三级联动,(两种实现方式的前提都是要用此数据结构,以及类型定义)
HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zbw_apple

可以参考以下demo:

// xxx.ets
class bottom {
  bottom:number = 50
}
let bott:bottom = new bottom()

@Entry
@Component
struct TextPickerExample {
  private select: number = 1
  private apfruits: string[] = ['apple1', 'apple2', 'apple3', 'apple4']
  private orfruits: string[] = ['orange1', 'orange2', 'orange3', 'orange4']
  private pefruits: string[] = ['peach1', 'peach2', 'peach3', 'peach4']
  private multi: string[][] = [this.apfruits, this.orfruits, this.pefruits]
  private json : string = '[{' +
    '      "code": "110000",' +
    '      "name": "北京市",' +
    '      "level": "省级",' +
    '      "children": [{' +
    '        "code": "110100",' +
    '        "name": "北京市",' +
    '        "level": "地级",' +
    '        "children": [{' +
    '          "code": "110101",' +
    '          "name": "东城区",' +
    '          "level": "县级",' +
    '          "children": []' +
    '        }, {' +
    '          "code": "110102",' +
    '          "name": "西城区",' +
    '          "level": "县级",' +
    '          "children": []' +
    '        }, {' +
    '          "code": "110105",' +
    '          "name": "朝阳区",' +
    '          "level": "县级",' +
    '          "children": []' +
    '        }, {' +
    '          "code": "110106",' +
    '          "name": "丰台区",' +
    '          "level": "县级",' +
    '          "children": []' +
    '        }, {' +
    '          "code": "110107",' +
    '          "name": "石景山区",' +
    '          "level": "县级",' +
    '          "children": []' +
    '        }, {' +
    '          "code": "110108",' +
    '          "name": "海淀区",' +
    '          "level": "县级",' +
    '          "children": []' +
    '        }, {' +
    '          "code": "110109",' +
    '          "name": "门头沟区",' +
    '          "level": "县级",' +
    '          "children": []' +
    '        }, {' +
    '          "code": "110111",' +
    '          "name": "房山区",' +
    '          "level": "县级",' +
    '          "children": []' +
    '        }, {' +
    '          "code": "110112",' +
    '          "name": "通州区",' +
    '          "level": "县级",' +
    '          "children": []' +
    '        }, {' +
    '          "code": "110113",' +
    '          "name": "顺义区",' +
    '          "level": "县级",' +
    '          "children": []' +
    '        }, {' +
    '          "code": "110114",' +
    '          "name": "昌平区",' +
    '          "level": "县级",' +
    '          "children": []' +
    '        }, {' +
    '          "code": "110115",' +
    '          "name": "大兴区",' +
    '          "level": "县级",' +
    '          "children": []' +
    '        }, {' +
    '          "code": "110116",' +
    '          "name": "怀柔区",' +
    '          "level": "县级",' +
    '          "children": []' +
    '        }, {' +
    '          "code": "110117",' +
    '          "name": "平谷区",' +
    '          "level": "县级",' +
    '          "children": []' +
    '        }, {' +
    '          "code": "110118",' +
    '          "name": "密云区",' +
    '          "level": "县级",' +
    '          "children": []' +
    '        }, {' +
    '          "code": "110119",' +
    '          "name": "延庆区",' +
    '          "level": "县级",' +
    '          "children": []' +
    '        }, {' +
    '          "code": "110100",' +
    '          "name": "北京市本级",' +
    '          "level": "地级",' +
    '          "children": []' +
    '        }]' +
    '      }]' +
    '    }]'

  private cascade: 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  ssdsdsds : TextCascadePickerRangeContent[] = []

  aboutToAppear(): void {
    let jsonre = this.json.replaceAll('name','text')
    this.ssdsdsds = JSON.parse(jsonre)
    console.log(JSON.stringify(this.ssdsdsds))
    console.log(JSON.stringify(this.ssdsdsds[0].text))
  }
  build() {
    Column() {

      //TextPicker({ range: this.cascade })
      TextPicker({ range: this.ssdsdsds })
        .onChange((value: string | string[], index: number | number[]) => {
          console.info('TextPicker 多列联动:onChange ' + JSON.stringify(value) + ', ' + 'index: ' + JSON.stringify(index))
        })
    }
  }
}
分享
微博
QQ
微信
回复
2天前
相关问题
HarmonyOS 三级省市县地址级联选择demo
198浏览 • 1回复 待解决
HarmonyOS 需要二级联动的demo
486浏览 • 1回复 待解决
Image组件是否有三级缓存策略
1129浏览 • 1回复 待解决
HarmonyOS TextPicker多级联动的展示问题
182浏览 • 1回复 待解决
HarmonyOS TextPickerDialog多级联动问题
135浏览 • 1回复 待解决
HarmonyOS 如何实现级联选择省市区
684浏览 • 1回复 待解决
HarmonyOS 列表联动交互
134浏览 • 1回复 待解决
HarmonyOS 联动组件咨询
420浏览 • 1回复 待解决
HarmonyOS List联动滑动
144浏览 • 1回复 待解决
HarmonyOS list web scroll 联动
101浏览 • 1回复 待解决
HarmonyOS中的多设备联动如何实现?
327浏览 • 0回复 待解决
HarmonyOS Video组件与Slider组件联动
46浏览 • 1回复 待解决