鸿蒙中搜索功能(不是商品搜索)

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))
  }
}
  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值