鸿蒙实战开发-搜索功能实现案例
摘要 本文介绍了使用ArkTS实现模糊查询功能的示例,通过includes方法实现数据筛选,并利用PersistentStorage持久化存储搜索历史。示例包含搜索页面跳转、动态筛选渲染、历史记录保存等功能,展示了鸿蒙应用开发中常见的数据查询与存储操作。工程结构清晰,分为搜索组件、页面和数据模型三部分,适用于需要实现搜索功能的鸿蒙应用场景。
·
往期推文全新看点(文中附带全新鸿蒙5.0全栈学习笔录)
✏️ 鸿蒙应用开发与鸿蒙系统开发哪个更有前景?
✏️ 嵌入式开发适不适合做鸿蒙南向开发?看完这篇你就了解了~
✏️ 对于大前端开发来说,转鸿蒙开发究竟是福还是祸?
✏️ 鸿蒙岗位需求突增!移动端、PC端、IoT到底该怎么选?
✏️ 市场巨变,移动开发行业即将迎来“第二春”?
✏️ 记录一场鸿蒙开发岗位面试经历~
✏️ 持续更新中……
介绍
本示例介绍使用includes方法对数据实现模糊查询
效果图预览

使用说明
- 点击首页搜索框跳转到搜索页面
- 在搜索页面输入框中输入搜索的内容,下方列表自动根据搜索的内容进行筛选渲染
- 点击筛选后的列表跳转到相应的页面
- 跳转后会保存搜索历史,搜索历史使用持久化存储处理退出应用再次进入依旧存在
- 点击搜索历史可以跳转到相应页面
实现思路
- 通过include方法判读是否存在符合条件的数据。
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进行持久化数据存储。
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);
})
高性能知识点
不涉及
工程结构&模块类型
SearchComponent // har类型(默认使用har类型,如果使用hsp类型请说明原因)
|---model
| |---ListData.ets // 筛选数据模型
|---SearchComponent.ets // 搜索组件
|---SearchPage.ets // 搜索页面

火山引擎开发者社区是火山引擎打造的AI技术生态平台,聚焦Agent与大模型开发,提供豆包系列模型(图像/视频/视觉)、智能分析与会话工具,并配套评测集、动手实验室及行业案例库。社区通过技术沙龙、挑战赛等活动促进开发者成长,新用户可领50万Tokens权益,助力构建智能应用。
更多推荐
所有评论(0)