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

Cordova与OpenHarmony高级搜索系统

欢迎大家加入开源鸿蒙跨平台开发者社区,一起共建开源鸿蒙跨平台生态。

高级搜索系统概述

高级搜索系统为用户提供了更精细的搜索控制。在Cordova框架与OpenHarmony系统的结合下,我们需要实现一个功能完整的高级搜索系统,支持多条件组合搜索和复杂的查询逻辑。

高级搜索查询模型

classAdvancedSearchQuery{constructor(){this.conditions=[];this.operator='AND';// AND 或 ORthis.sortBy='relevance';// relevance, date, namethis.sortOrder='desc';// asc 或 desc}addCondition(field,operator,value){this.conditions.push({field:field,operator:operator,// =, !=, >, <, >=, <=, contains, startsWith, endsWithvalue:value});}removeCondition(index){this.conditions.splice(index,1);}clearConditions(){this.conditions=[];}}classAdvancedSearchEngine{constructor(){this.query=newAdvancedSearchQuery();}executeQuery(){letresults=[];// 搜索植物results=results.concat(this.searchPlants());// 搜索记录results=results.concat(this.searchRecords());// 应用排序results=this.sortResults(results);returnresults;}searchPlants(){returnplants.filter(plant=>this.evaluateConditions(plant));}searchRecords(){constallRecords=[...wateringManager.records,...fertilizingManager.records,...pruningManager.records];returnallRecords.filter(record=>this.evaluateConditions(record));}evaluateConditions(item){if(this.query.conditions.length===0)returntrue;constresults=this.query.conditions.map(condition=>this.evaluateCondition(item,condition));if(this.query.operator==='AND'){returnresults.every(r=>r);}else{returnresults.some(r=>r);}}evaluateCondition(item,condition){constvalue=item[condition.field];switch(condition.operator){case'=':returnvalue===condition.value;case'!=':returnvalue!==condition.value;case'>':returnvalue>condition.value;case'<':returnvalue<condition.value;case'>=':returnvalue>=condition.value;case'<=':returnvalue<=condition.value;case'contains':returnString(value).includes(String(condition.value));case'startsWith':returnString(value).startsWith(String(condition.value));case'endsWith':returnString(value).endsWith(String(condition.value));default:returnfalse;}}sortResults(results){returnresults.sort((a,b)=>{letcompareValue=0;if(this.query.sortBy==='date'){compareValue=newDate(a.date)-newDate(b.date);}elseif(this.query.sortBy==='name'){compareValue=String(a.name).localeCompare(String(b.name));}returnthis.query.sortOrder==='asc'?compareValue:-compareValue;});}}

这个高级搜索系统定义了AdvancedSearchQuery和AdvancedSearchEngine类。支持多条件组合搜索和复杂的查询逻辑。

与OpenHarmony数据库的集成

functionexecuteAdvancedSearchInDatabase(query){cordova.exec(function(result){console.log("高级搜索完成");renderAdvancedSearchResults(result);},function(error){console.error("搜索失败:",error);},"DatabasePlugin","advancedSearch",[{conditions:query.conditions,operator:query.operator,sortBy:query.sortBy,sortOrder:query.sortOrder}]);}

这段代码展示了如何与OpenHarmony的数据库进行高级搜索。

高级搜索页面

functionrenderAdvancedSearchPage(){constcontainer=document.getElementById('page-container');container.innerHTML=`<div class="advanced-search-page"> <h2>高级搜索</h2> <div class="search-builder"> <div class="conditions-section"> <h3>搜索条件</h3> <div id="conditions-list"></div> <button onclick="addSearchCondition()">➕ 添加条件</button> </div> <div class="operator-section"> <label> <input type="radio" name="operator" value="AND" checked> 所有条件都满足 (AND) </label> <label> <input type="radio" name="operator" value="OR"> 任意条件满足 (OR) </label> </div> <div class="sort-section"> <label>排序方式: <select id="sort-by"> <option value="relevance">相关性</option> <option value="date">日期</option> <option value="name">名称</option> </select> </label> <label> <input type="radio" name="sort-order" value="desc" checked> 降序 </label> <label> <input type="radio" name="sort-order" value="asc"> 升序 </label> </div> <div class="search-actions"> <button onclick="executeAdvancedSearch()">🔍 搜索</button> <button onclick="resetAdvancedSearch()">重置</button> </div> </div> <div id="advanced-search-results"></div> </div>`;renderConditionsList();}functionaddSearchCondition(){constconditionsList=document.getElementById('conditions-list');constconditionIndex=conditionsList.children.length;constconditionDiv=document.createElement('div');conditionDiv.className='condition-item';conditionDiv.id=`condition-${conditionIndex}`;conditionDiv.innerHTML=`<select class="condition-field" onchange="updateConditionOperators(${conditionIndex})"> <option value="">选择字段</option> <option value="name">植物名称</option> <option value="species">物种</option> <option value="location">位置</option> <option value="health">健康状态</option> <option value="date">日期</option> <option value="amount">数量</option> </select> <select class="condition-operator"> <option value="=">=</option> <option value="!=">!=</option> <option value=">">></option> <option value="<"><</option> <option value="contains">包含</option> <option value="startsWith">开头是</option> <option value="endsWith">结尾是</option> </select> <input type="text" class="condition-value" placeholder="输入值"> <button onclick="removeSearchCondition(${conditionIndex})">✕</button>`;conditionsList.appendChild(conditionDiv);}functionremoveSearchCondition(index){constconditionDiv=document.getElementById(`condition-${index}`);if(conditionDiv){conditionDiv.remove();}}functionexecuteAdvancedSearch(){constquery=newAdvancedSearchQuery();// 收集条件constconditionItems=document.querySelectorAll('.condition-item');conditionItems.forEach(item=>{constfield=item.querySelector('.condition-field').value;constoperator=item.querySelector('.condition-operator').value;constvalue=item.querySelector('.condition-value').value;if(field&&value){query.addCondition(field,operator,value);}});// 获取操作符constoperatorRadios=document.querySelectorAll('input[name="operator"]');operatorRadios.forEach(radio=>{if(radio.checked){query.operator=radio.value;}});// 获取排序选项query.sortBy=document.getElementById('sort-by').value;constsortOrderRadios=document.querySelectorAll('input[name="sort-order"]');sortOrderRadios.forEach(radio=>{if(radio.checked){query.sortOrder=radio.value;}});// 执行搜索constsearchEngine=newAdvancedSearchEngine();searchEngine.query=query;constresults=searchEngine.executeQuery();renderAdvancedSearchResults(results);}functionresetAdvancedSearch(){document.getElementById('conditions-list').innerHTML='';document.getElementById('advanced-search-results').innerHTML='';renderAdvancedSearchPage();}

这个函数创建高级搜索页面,允许用户添加多个搜索条件并设置排序选项。

搜索结果展示

functionrenderAdvancedSearchResults(results){constresultsContainer=document.getElementById('advanced-search-results');resultsContainer.innerHTML=`<div class="results-header"> <h3>搜索结果</h3> <p>找到${results.length}个结果</p> </div>`;if(results.length===0){resultsContainer.innerHTML+='<p class="empty-message">未找到匹配的结果</p>';return;}constresultsList=document.createElement('div');resultsList.className='results-list';results.forEach(result=>{constresultItem=document.createElement('div');resultItem.className='result-item';if(result.name){resultItem.innerHTML=`<h4>${result.name}</h4> <p>${result.species||result.type||''}</p> <p>${result.location||result.date||''}</p>`;}else{resultItem.innerHTML=`<h4>${result.plantId}</h4> <p>类型:${result.type||'记录'}</p> <p>日期:${newDate(result.date).toLocaleDateString('zh-CN')}</p>`;}resultsList.appendChild(resultItem);});resultsContainer.appendChild(resultsList);}

这个函数负责渲染高级搜索的结果。

搜索模板

classSearchTemplate{constructor(name,conditions){this.id='template_'+Date.now();this.name=name;this.conditions=conditions;}}classSearchTemplateManager{constructor(){this.templates=[];this.loadFromStorage();}saveTemplate(name,conditions){consttemplate=newSearchTemplate(name,conditions);this.templates.push(template);this.saveToStorage();returntemplate;}loadTemplate(templateId){returnthis.templates.find(t=>t.id===templateId);}deleteTemplate(templateId){this.templates=this.templates.filter(t=>t.id!==templateId);this.saveToStorage();}}

这个SearchTemplateManager类管理搜索模板。用户可以保存常用的搜索条件组合,以便快速重复使用。

总结

高级搜索系统为用户提供了强大的搜索能力。通过支持多条件组合搜索和复杂的查询逻辑,我们可以创建一个功能完整的高级搜索系统,帮助用户精确找到所需的信息。

http://www.cnnetsun.cn/news/175338.html

相关文章:

  • 备份恢复-Cordovaopenharmony本地安全方案
  • 创建目标模块 Cordova 与 OpenHarmony 混合开发实战
  • 解决MQ消息丢失问题的5种方案
  • 芜湖,千兆网络下载速率只有10MB秒,过的什么苦日子
  • AI一周大事盘点(2025年12月14日~2025年12月20日)
  • K3s + Sysbox:让容器拥有“虚拟机的灵魂”
  • 8 个降AI率工具推荐,继续教育学生必备
  • 从开发一个AI美女聊天群组开始
  • 12.2K Star 爆火!开源免费的 FileConverter:右键一键搞定音视频 / 图片 / 文档转换,告别多工具切换
  • Java毕设项目:基于springboot的养宠物指南服务平台系统的设计与实现(源码+文档,讲解、调试运行,定制等)
  • 10 个降AI率工具,继续教育学生高效避坑指南
  • Java毕设项目推荐-基于SpringBoot的演唱会门票在线预定系统的设计与实现基于springboot的演唱会购票系统的设计与实现【附源码+文档,调试定制服务】
  • 升压芯片很简单(一),快速选择升压芯片+利用升压芯片设计LED电源
  • 基于web的人才招聘网站设计 nodejs vue
  • 测试20个降AI率工具后,我找到了2个去ai痕迹效果好的网站,还有免费降AI额度。
  • Thinkphp和Laravel在线点餐系统的设计与实现vue
  • 现代cpp在传统内存分配上的改进
  • Java毕设项目:基于springboot的物业报修系统的设计与实现(源码+文档,讲解、调试运行,定制等)
  • 【计算机毕业设计案例】基于springboot的物业报修系统的设计与实现线上化的报修管理平台(程序+文档+讲解+定制)
  • Java毕设选题推荐:基于springboot的社区团购系统的设计与实现、拼团下单、配送调度、资金结算【附源码、mysql、文档、调试+代码讲解+全bao等】
  • Java计算机毕设之基于springboot的幼儿园管理系统的设计与实现为幼儿园(含普惠园、民办园、连锁园)设计的 “家园共育 + 日常运营 + 安全监管(完整前后端代码+说明文档+LW,调试定制等)
  • I/O多路复用
  • 视频播放器PotPlayer下载安装教程:超详细图文步骤(PC+安卓)
  • Semantic Kernel 实战系列(六) - Memory与向量存储
  • 一个基于 .NET MAUI 的开箱即用的 UI 组件库,可快速搭建面向业务的应用程序界面!
  • Semantic Kernel 实战系列(七) - 高级主题 - Agents 与多代理系统
  • LeetCode每日一题——K个一组翻转链表
  • 大模型后训练:中美路径与商业闭环|附56页PDF文件下载
  • 震惊!选对云服务器代理商,这5个关键指标必须知道!
  • 2025年度复盘与总结