中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
select的事件里只有选择选项后触发的事件,现在select的option是需要调用接口动态查询的,有没有办法可以实现
微信扫码分享
// xxx.ets @Entry @Component struct SelectExample { @State text: string = "TTTTT" @State index: number = 2 @State space: number = 8 @State arrowPosition: ArrowPosition = ArrowPosition.END @State options: Array<SelectOption> = [ { value: 'aaa', icon: $r("app.media.app_icon") }, { value: 'bbb', icon: $r("app.media.app_icon") }, { value: 'ccc', icon: $r("app.media.app_icon") }, { value: 'ddd', icon: $r("app.media.app_icon") }] @State options2: Array<SelectOption> = [] @State selected: string = '' @State @Watch('onSelectedIndexUpData') selectedIndex: number = -1 onSelectedIndexUpData() { console.info('数据发生变化') if (this.selectedIndex < 0) { return } this.options2.push(this.options[this.selectedIndex]) } build() { Row() { TextInput({ placeholder: 'input ...', text: this.selected }) .onChange((value: string) => { this.selected = value let index = this.options.findIndex(item => item.value === value) this.selectedIndex = index >= 0 ? index : -1 console.info(index.toString()) }) .width('60%') Select(this.options2)//.selected(this.index) .value(this.text) .font({ size: 16, weight: 500 }) .onSelect((index: number, text?: string | undefined) => { console.info('Select:' + index) this.index = index; if (text) { this.text = text; } this.options2.pop() this.selectedIndex = -1 }) }.width('100%') } }