前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >纯血鸿蒙APP实战开发——搜索功能实现案例

纯血鸿蒙APP实战开发——搜索功能实现案例

原创
作者头像
小帅聊鸿蒙
发布2024-12-24 20:56:00
发布2024-12-24 20:56:00
2710
举报
文章被收录于专栏:鸿蒙开发笔记鸿蒙开发笔记

介绍

本示例介绍使用includes方法对数据实现模糊查询

效果图预览

使用说明

  1. 点击首页搜索框跳转到搜索页面
  2. 在搜索页面输入框中输入搜索的内容,下方列表自动根据搜索的内容进行筛选渲染
  3. 点击筛选后的列表跳转到相应的页面
  4. 跳转后会保存搜索历史,搜索历史使用持久化存储处理退出应用再次进入依旧存在
  5. 点击搜索历史可以跳转到相应页面

实现思路

  1. 通过include方法判读是否存在符合条件的数据。
代码语言:ts
复制
  searchFunc(value: string) {
    let newListData: ListData[] = [];
    if (this.searchListData !== undefined) {
      for (let i = 0; i < this.searchListData.length; i++) {
        // 通过includes对输入的字符进行查询
        if (this.searchListData[i].name.toLowerCase().includes(value.toLowerCase())) {
          newListData.push(this.searchListData[i])
        }
      }
    }
    this.listData = newListData
  }

2通过PersistentStorage进行持久化数据存储。

代码语言:ts
复制
  PersistentStorage.persistProp('searchHistoryData', [])
  @StorageLink('searchHistoryData') searchHistoryData: ListData[] = []
  ListItem() {
    Column() {
      Row() {
        Image($r('app.media.search'))
          .width($r('app.string.search_list_image_width'))
        Text(item.name)
          .fontSize($r('app.string.search_history_font_size2'))
          .margin({ left: $r('app.string.search_history_text_padding_margin2') })
      }

      Divider()
        .width('100%')
        .height(1)
        .margin({ top: $r('app.string.search_history_text_padding_margin1') })
    }
    .width('100%')
    .alignItems(HorizontalAlign.Start)
  }
  .width('100%')
  .margin({ top: $r('app.string.search_history_text_padding_margin1') })
  .onClick(() => {
     if (this.searchHistoryData.includes(item)) {
       return;
     }
     // 更新搜索历史数据
     this.searchHistoryData.push(item);
     // 调用动态路由相关方法实现页面跳转
     DynamicsRouter.push(item.routerInfo, item.param);
     })

高性能知识点

不涉及

工程结构&模块类型

代码语言:shell
复制
   SearchComponent                                 // har类型(默认使用har类型,如果使用hsp类型请说明原因)
   |---model
   |   |---ListData.ets                            // 筛选数据模型
   |---SearchComponent.ets                         // 搜索组件
   |---SearchPage.ets                              // 搜索页面

写在最后

如果你觉得这篇内容对你还蛮有帮助,我想邀请你帮我三个小忙:

  • 点赞,转发,有你们的 『点赞和评论』,才是我创造的动力;
  • 关注小编,同时可以期待后续文章ing🚀,不定期分享原创知识;
  • 想要获取更多完整鸿蒙最新学习知识点,可关注B站:码牛课堂;

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 介绍
  • 效果图预览
  • 实现思路
  • 高性能知识点
  • 工程结构&模块类型
  • 写在最后
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档