当前位置: 首页 > backend >正文

《仿盒马》app开发技术分享-- 商品搜索页(顶部搜索bar热门搜索)(端云一体)

开发准备

随着开发功能的逐渐深入,我们的应用逐渐趋于完善,现在我们需要继续在首页给没有使用按钮以及组件添加对应的功能,这一节我们要实现的功能是商品搜索页面,这个页面我们从上到下开始实现功能,首先就是一个搜索的bar,然后是一个系统推荐的热门搜索列表。

功能分析

要实现顶部的搜索bar其实还是非常简单的,我们只需要使用对应的row布局去填充对应的组件,然后根据输入状态来实现对应组建的展示和隐藏即可,热门搜索列表我们需要在云端新建对应的表,填充数据后,进行云数据库数据的请求

代码实现

首先我们先创建对应的表

{"objectTypeName": "search_hot_txt","fields": [{"fieldName": "id", "fieldType": "Integer", "notNull": true, "belongPrimaryKey": true},{"fieldName": "txt", "fieldType": "String" }],"indexes": [{"indexName": "field1Index", "indexList": [{"fieldName":"id","sortType":"ASC"}]}],"permissions": [{"role": "World", "rights": ["Read"]},{"role": "Authenticated", "rights": ["Read", "Upsert"]},{"role": "Creator", "rights": ["Read", "Upsert", "Delete"]},{"role": "Administrator", "rights": ["Read", "Upsert", "Delete"]}]
}

对应的实体类


class SearchHotTxt {id: number;txt: string;constructor() {}getFieldTypeMap():  Map<string, string> {let fieldTypeMap = new Map<string, string>();fieldTypeMap.set('id', 'Integer');fieldTypeMap.set('txt', 'String');return fieldTypeMap;}getClassName(): string {return 'search_hot_txt';}getPrimaryKeyList(): string[] {let primaryKeyList: string[] = [];primaryKeyList.push('id');return primaryKeyList;}getIndexList(): string[] {let indexList: string[] = [];indexList.push('id');return indexList;}getEncryptedFieldList(): string[] {let encryptedFieldList: string[] = [];return encryptedFieldList;}setId(id: number): void {this.id = id;}getId(): number  {return this.id;}setTxt(txt: string): void {this.txt = txt;}getTxt(): string  {return this.txt;}static parseFrom(inputObject: any): SearchHotTxt {let result = new SearchHotTxt();if (!inputObject) {return result;}if (inputObject.id) {result.id = inputObject.id;}if (inputObject.txt) {result.txt = inputObject.txt;}return result;}
}export { SearchHotTxt };

对应的db类

import { cloudDatabase } from '@kit.CloudFoundationKit';class search_hot_txt extends cloudDatabase.DatabaseObject {public id: number;public txt: string;public naturalbase_ClassName(): string {return 'search_hot_txt';}
}export { search_hot_txt };

之后我们创建对应的商品搜索页面,在页面中实现一个bar

//先创建好对应的变量
@State text: string = ''controller: TextInputController = new TextInputController()@State searchTxt:SearchHotTxt[]=[]@State flag:boolean=false@State isSearch:boolean=false@State columns: number = 2

实现布局

 Row(){Image($r('app.media.left_back')).height(20).width(20).onClick(()=>{router.back()})TextInput({ text: this.text, placeholder: '输入商品名搜索', controller: this.controller }).placeholderColor(Color.White).placeholderFont({ size: 16, weight: 400 }).caretColor(Color.White).width(200).fontSize(16).fontColor(Color.White).onChange((value: string) => {this.text = valueif (value.length==0) {this.isSearch=false}})Text("搜索").border({width:1,radius:10,color:Color.White}).fontSize(14).fontColor("#ffffff").padding({left:13,right:13,top:5,bottom:5}).borderRadius(10).onClick(async ()=>{if (this.text.trim()==''&&this.text.length==0) {this.isSearch=falseshowToast("搜索内容不能为空")}else {this.isSearch=true}})}.justifyContent(FlexAlign.SpaceBetween).width('100%').padding({top:10,bottom:10,left:15,right:15}).expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP]).backgroundColor("#ff0000")

从云数据库查询出对应的数据

 async aboutToAppear(): Promise<void> {const history = await StorageUtils.getAll("history_list")if (history!=''&&history!=undefined) {this.searchHistoryList=JSON.parse(history)}let condition = new cloudDatabase.DatabaseQuery(search_hot_txt);let listData = await databaseZone.query(condition);let json = JSON.stringify(listData)let data1:SearchHotTxt[]= JSON.parse(json)this.searchTxt=data1this.flag=true}

展示搜索列表

 Text("热门搜索").width('100%').fontSize(16).fontColor("#000000").fontWeight(FontWeight.Bold).padding({left:15,top:15}) Flex({wrap:FlexWrap.Wrap}){ForEach(this.searchTxt,(item:SearchHotTxt,index:number)=>{Text(item.txt).backgroundColor("#ffe7e5e5").fontColor("#000000").fontWeight(FontWeight.Bold).fontSize(16).padding(10).margin({top:10,left:10}).borderRadius(5).onClick(()=>{this.text=item.txt})})}

我们执行一下代码看看效果
在这里插入图片描述

http://www.xdnf.cn/news/12038.html

相关文章:

  • 中级统计师-经济学基础知识-第一章 经济学基础
  • android与Qt类比
  • 重要通知:6月申报纳税期限延至6月16日(附2025年办税日历 图文版)
  • 树莓派超全系列教程文档--(53)如何使用 SCP 共享文件
  • 嵌入式开发之STM32学习笔记day18
  • 手写Promise实例方法finally
  • 肺癌靶向治疗耐药困境下的新探索
  • LLM应用开发(九)- 幻觉及如何缓解
  • Authentication failed(切换了新的远程仓库tld)
  • 区块链技术发展现状与应用前景分析
  • Rocket客户端消息确认机制
  • JAVA:String类详解
  • 华为云Flexus+DeepSeek征文|Flexus云服务器单机部署+CCE容器高可用部署快速搭建生产级的生成式AI应用
  • (LeetCode 每日一题)3403. 从盒子中找出字典序最大的字符串 I (贪心+枚举)
  • 12.3Swing控件1
  • Java并发编程实战 Day 7:并发集合类详解
  • Docker轻松搭建Neo4j+APOC环境
  • 《PyTorch Hub:解锁深度学习模型的百宝箱》
  • 物流数据接口新玩法:跨境订单处理效率提升200%
  • echarts显示/隐藏标签的同时,始终显示饼图中间文字
  • 简数采集技巧之快速获取特殊链接网址URL方法
  • Playwright 测试框架 - Python
  • 软件工程专业的本科生应该具备哪些技能
  • 【Bluedroid】蓝牙启动之gatt_init 流程源码解析
  • DrissionPage爬虫包实战分享
  • 汽车加气站操作工证考试重点
  • 文献阅读|基于PSMA PET/CT与mpMRI多模态深度学习预测前列腺癌的不良病变
  • Spring AI 之工具调用
  • cpp多线程学习
  • 无人机光纤FC接口模块技术分析