import { JSON } from '@kit.ArkTS'; class type { id: number = 0 name: string = '' } @Entry @Component struct SearchPage { @State searchText: string = ''; @State filteredItems: type[] = []; // 假设这是你的数据源 // 假设这是你的初始数据 @State allItems: type[] = [ { id: 1, name: 'Apple' }, { id: 2, name: 'Banana' }, { id: 1, name: 'nicai' }, { id: 2, name: 'bucai' }, { id: 1, name: 'yi' }, { id: 2, name: 'er' }, { id: 1, name: 'san' }, { id: 2, name: 'si' }, { id: 1, name: 'wu' }, { id: 2, name: 'liu' }, ]; @State textvalue: string = '' aboutToAppear(): void { this.filteredItems = this.allItems } build() { Column() { TextInput({ placeholder: "请输入" }) .width('100%') .margin({ top: 10 }) .onChange((value: string) => { this.textvalue = value this.onSearchChange(this.textvalue) }) Button("搜索").onClick(() => { this.onSearchChange(this.textvalue) }) Column() { Text('结果') ForEach(this.filteredItems, (item: type) => { Text(item.name) .fontSize(16) .margin({ bottom: 5 }) }) } } .padding(16) } private onSearchChange(newValue: string) { this.searchText = newValue; this.updateFilteredItems(); } private updateFilteredItems() { // 根据搜索词过滤数据 this.filteredItems = this.allItems.filter(item => item.name.toLowerCase().includes(this.searchText.toLowerCase())); console.log("aaaaaaaaa" + JSON.stringify(this.filteredItems)) } }