news 2026/3/2 6:58:50

Cordova与OpenHarmony高级搜索系统

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
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类管理搜索模板。用户可以保存常用的搜索条件组合,以便快速重复使用。

总结

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

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/2/28 21:04:58

初次约会“社交算法”:高效对话框架让好感度指数级增长

一、需求分析与背景设定在情感社交场景中&#xff0c;初次约会可被视为一次关键的用户体验测试。许多技术从业者面临一个共同痛点&#xff1a;在代码世界游刃有余&#xff0c;但在面对面的情感交流中却常常陷入“系统异常”。本文将从结构化思维出发&#xff0c;为你构建一套高…

作者头像 李华
网站建设 2026/3/1 18:16:13

进度跟踪模块 Cordova 与 OpenHarmony 混合开发实战

&#x1f4cc; 概述 进度跟踪模块允许用户跟踪目标的完成进度。该模块集成了 Cordova 框架与 OpenHarmony 原生能力&#xff0c;提供了完整的进度更新和可视化展示。用户可以查看目标的当前进度、剩余时间和完成预测。模块支持进度的快速更新和历史记录查看。 &#x1f517; 完…

作者头像 李华
网站建设 2026/2/25 18:21:12

算法学习02|单调队列(上)学习总结

依旧是学习左神的课程&#xff1a;单调队列&#xff08; 上&#xff09; 单调队列的定义 单调队列&#xff0c;顾名思义&#xff0c;在实现一个双端队列&#xff08;队头队尾都可以插入、弹出元素&#xff09;的基础上&#xff0c;保持队列的数据从大到小&#xff08;从小到大…

作者头像 李华
网站建设 2026/2/27 19:16:45

Java并发编程基础:从线程管理到高并发应用实践

1. 理解线程&#xff1a;多任务执行的基石 1.1 什么是线程&#xff1f; 在现代操作系统中&#xff0c;进程是资源分配的基本单位&#xff0c;而线程是CPU调度的最小单位。可以把进程想象成一家公司&#xff0c;线程就是公司里的员工。 /** * 演示Java程序天生就是多线程程序 …

作者头像 李华
网站建设 2026/2/26 22:39:35

记一次 Kubebuilder Operator 开发中的 CRD 注解超限问题

概念厘清&#xff1a;注解、CSA 与三路合并的来龙去脉要理解这个问题&#xff0c;需要先弄清楚几个关键概念。1. annotations 是什么&#xff1f;在 Kubernetes 中&#xff0c;注解是与对象关联的键值对&#xff0c;用于存储非标识性的元数据。这些信息可以被工具、库或控制器读…

作者头像 李华
网站建设 2026/2/28 14:57:43

约翰·伯格的资产配置理论

约翰伯格的资产配置理论 关键词:约翰伯格、资产配置理论、投资组合、风险分散、长期投资 摘要:本文深入探讨了约翰伯格的资产配置理论。该理论强调通过合理的资产配置实现风险分散和长期稳定的投资回报。文章首先介绍了理论提出的背景,包括目的、预期读者和文档结构等内容。…

作者头像 李华