前端Vue自定义带历史记录的搜索框组件searchBar 支持搜索输入框清空 搜索历史存储记录清除,下载完整代码请访问uni-app插件市场地址:https://ext.dcloud.net.cn/plugin?id=13343

效果图如下:

cc-hisSearchBar

使用方法


// 不同的业务功能历史记录设置不同存储key const kStorageKey = "storageKey" // 执行搜索事件 doSearch() { if (this.keyword == '') { uni.showToast({ title: '请输入要搜索的内容', icon: 'none' }); return; } // if (this.keyword && this.keyword !== '') { this.saveKeyword(this.keyword); //保存为历史 } uni.showLoading({ title: '搜索中...' }); this.setData({ keywordList: [] }); uni.hideLoading() uni.showModal({ title: "温馨提示", content: '搜索的内容 = ' + this.keyword }) }, //点击历史记录搜索 doHisSearch(item) { console.log(item) uni.showModal({ title: '点击的历史搜索条目', content: '点击的历史搜索条目 = ' + item }) },

HTML代码实现部分


<template> <view class="content"> <view class="search-box"> <view class="input-box"> <input type="text" adjust-position="true" v-model="keyword" :placeholder="defaultKeyword" placeholder-class="search_text" @confirm="doSearch" data-key="false" confirm-type="search" @input="onclear"></input> <image src="/static/close.png" @tap="clears" v-if="shows == true"></image> </view> <view class="search-btn" @tap="doSearch" data-key="false" :style="'background:' + colors">搜索</view> </view> <view class="search-keyword"> <scroll-view class="keyword-box" scroll-y> <view class="keyword-block" v-if="oldKeywordList.length>0"> <view class="keyword-list-header"> <view>历史搜索</view> <view> <image @tap="oldDelete" src="/static/delete.png"></image> </view> </view> <view class="keyword"> <view v-for="(item, index) in oldKeywordList" :key="index" @tap="doHisSearch(item)">{{item}} </view> </view> </view> </scroll-view> </view> </view> </template> <script> // 不同的业务功能历史记录设置不同存储key const kStorageKey = "storageKey" import Vue from 'vue' Vue.mixin({ methods: { setData: function(obj, callback) { let that = this; const handleData = (tepData, tepKey, afterKey) => { tepKey = tepKey.split('.'); tepKey.forEach(item => { if (tepData[item] === null || tepData[item] === undefined) { let reg = /^[0-9]+$/; tepData[item] = reg.test(afterKey) ? [] : {}; tepData = tepData[item]; } else { tepData = tepData[item]; } }); return tepData; }; const isFn = function(value) { return typeof value == 'function' || false; }; Object.keys(obj).forEach(function(key) { let val = obj[key]; key = key.replace(/\]/g, '').replace(/\[/g, '.'); let front, after; let index_after = key.lastIndexOf('.'); if (index_after != -1) { after = key.slice(index_after + 1); front = handleData(that, key.slice(0, index_after), after); } else { after = key; front = that; } if (front.$data && front.$data[after] === undefined) { Object.defineProperty(front, after, { get() { return front.$data[after]; }, set(newValue) { front.$data[after] = newValue; that.$forceUpdate(); }, enumerable: true, configurable: true }); front[after] = val; } else { that.$set(front, after, val); } }); isFn(callback) && this.$nextTick(callback); } } }); var app = getApp(); export default { data() { return { defaultKeyword: "", keyword: "", oldKeywordList: [], hotKeywordList: [], keywordList: [], forbid: '', shows: false, colors: '' }; }, components: {}, props: {}, /** * 生命周期函数--监听页面加载 */ onLoad: function(options) { this.setData({ colors: app.globalData.newColor }); this.init(); }, methods: { init() { this.loadDefaultKeyword(); this.loadOldKeyword(); }, loadDefaultKeyword() { //定义默认搜索关键字,可以自己实现ajax请求数据再赋值,用户未输入时,以水印方式显示在输入框,直接不输入内容搜索会搜索默认关键字 this.setData({ defaultKeyword: "请输入搜索的内容" }); }, //加载历史搜索,自动读取本地Storage loadOldKeyword() { uni.getStorage({ key: kStorageKey, success: res => { var OldKeys = JSON.parse(res.data); this.setData({ oldKeywordList: OldKeys }); } }); }, // 执行搜索事件 doSearch() { if (this.keyword == '') { uni.showToast({ title: '请输入要搜索的内容', icon: 'none' }); return; } // if (this.keyword && this.keyword !== '') { this.saveKeyword(this.keyword); //保存为历史 } uni.showLoading({ title: '搜索中...' }); this.setData({ keywordList: [] }); uni.hideLoading() uni.showModal({ title: "温馨提示", content: '搜索的内容 = ' + this.keyword }) // uni.showToast({ // title: '暂无结果', // icon: 'none' // }) }, onclear(e) { this.setData({ keyword: e.detail.value, shows: e.detail.value !== '' ? true : false }); }, //保存关键字到历史记录 saveKeyword(keyword) { uni.getStorage({ key: kStorageKey, success: res => { var OldKeys = JSON.parse(res.data); var findIndex = OldKeys.indexOf(keyword); if (findIndex == -1) { OldKeys.unshift(keyword); } else { OldKeys.splice(findIndex, 1); OldKeys.unshift(keyword); } //最多10个纪录 OldKeys.length > 10 && OldKeys.pop(); uni.setStorage({ key: kStorageKey, data: JSON.stringify(OldKeys) }); this.setData({ oldKeywordList: OldKeys //更新历史搜索 }); }, fail: e => { var OldKeys = [keyword]; uni.setStorage({ key: kStorageKey, data: JSON.stringify(OldKeys) }); this.setData({ oldKeywordList: OldKeys //更新历史搜索 }); } }); }, oldDelete() { uni.showModal({ content: '确定清除历史搜索记录?', success: res => { if (res.confirm) { console.log('用户点击确定'); this.setData({ oldKeywordList: [] }); uni.removeStorage({ key: kStorageKey }); } else if (res.cancel) { console.log('用户点击取消'); } } }); }, clears() { this.setData({ keyword: '', keywordList: [], }); }, //点击历史记录搜索 doHisSearch(item) { console.log(item) uni.showModal({ title: '点击的历史搜索条目', content: '点击的历史搜索条目 = ' + item }) }, } }; </script> <style scoped lang="scss"> view { display: block; } .search-box { background-color: rgb(242, 242, 242); padding: 15upx 2.5%; display: flex; justify-content: space-between; position: sticky; top: 0; } .search-box .mSearch-input-box { width: 100%; } .search-box .input-box { width: 85%; flex-shrink: 1; display: flex; justify-content: center; align-items: center; } .search-box .search-btn { width: 15%; margin: 0 0 0 2%; display: flex; justify-content: center; align-items: center; flex-shrink: 0; font-size: 24upx; color: #fff; background: linear-gradient(to right, #4492EB, #3F92EE); border-radius: 60upx; } .search-box .input-box>input { width: 95%; height: 60upx; font-size: 24upx; -webkit-appearance: none; -moz-appearance: none; appearance: none; margin: 0; background-color: #ffffff; } .placeholder-class { color: #9e9e9e; } .search-keyword { width: 100%; background-color: rgb(242, 242, 242); } .keyword-list-box { height: calc(100vh - 110upx); padding-top: 10upx; border-radius: 20upx 20upx 0 0; background-color: #fff; } .keyword-entry-tap { background-color: #eee; } .keyword-entry { width: 94%; height: 80upx; margin: 0 3%; font-size: 30upx; color: #333; display: flex; justify-content: space-between; align-items: center; border-bottom: solid 1upx #e7e7e7; } .keyword-entry image { width: 60upx; height: 60upx; } .keyword-entry .keyword-text, .keyword-entry .keyword-img { height: 80upx; display: flex; align-items: center; } .keyword-entry .keyword-text { width: 90%; } .keyword-entry .keyword-img { width: 10%; justify-content: center; } .keyword-box { height: calc(100vh - 110upx); border-radius: 20upx 20upx 0 0; background-color: #fff; } .keyword-box .keyword-block { padding: 10upx 0; } .keyword-box .keyword-block .keyword-list-header { width: 94%; padding: 10upx 3%; font-size: 27upx; color: #333; display: flex; justify-content: space-between; } .keyword-box .keyword-block .keyword-list-header image { width: 40upx; height: 40upx; } .keyword-box .keyword-block .keyword { width: 94%; padding: 3px 3%; display: flex; flex-flow: wrap; justify-content: flex-start; } .keyword-box .keyword-block .hide-hot-tis { display: flex; justify-content: center; font-size: 28upx; color: #6b6b6b; } .keyword-box .keyword-block .keyword>view { display: flex; justify-content: center; align-items: center; border-radius: 38upx; padding: 0 20upx; margin: 10upx 20upx 10upx 0; height: 60upx; font-size: 24upx; background-color: rgb(242, 242, 242); color: #6b6b6b; line-height: 60upx; } .search_text { font-size: 24upx; color: #B6B6C6; } .input-box { position: relative; height: 60upx; font-size: 24upx; border: 0; border-radius: 60upx; -webkit-appearance: none; appearance: none; padding: 0 3%; margin: 0; background-color: #ffffff; } .input-box image { height: 40upx; width: 40upx; float: right; position: absolute; right: 2%; top: 50%; transform: translateY(-50%); z-index: 100; } </style>

前端Vue自定义带历史记录的搜索框组件searchBar 支持搜索输入框清空 搜索历史存储记录清除的更多相关文章

  1. vue自定义可输入的选择框组件

    vue自定义可输入的选择框组件 props: 属性 说明 类型 默认值 selectDataList 下拉框中的内容 Array 空数组([]) value 输入框中的内容 String 空字符串(& ...

  2. Android零基础入门第62节:搜索框组件SearchView

    原文:Android零基础入门第62节:搜索框组件SearchView 一.SearchView概述 SearchView是搜索框组件,它可以让用户在文本框内输入文字,并允许通过监听器监控用户输入,当 ...

  3. 第二百一十节,jQuery EasyUI,SearchBox(搜索框)组件

    jQuery EasyUI,SearchBox(搜索框)组件 学习要点: 1.加载方式 2.属性列表 3.方法列表 本节课重点了解 EasyUI 中 SearchBox(搜索框)组件的使用方法,这个组 ...

  4. SearchBox( 搜索框) 组件

    一. 加载方式//class 加载方式<input id="ss" class="easyui-searchbox" style="width: ...

  5. jQuery实现搜索框插件+豆瓣音乐接口实现豆瓣搜索框

    jQuery实现搜索框插件 豆瓣音乐接口实现豆瓣搜索框 豆瓣接口有时不稳定,网络请求会报400,不要惊慌.我主要是练习一下jQuery的JSONP和封装插件. <div class=" ...

  6. Nuxt/Vue自定义导航栏Topbar+标签栏Tabbar组件

    基于Vue.js实现自定义Topbar+Tabbar组件|仿咸鱼底部凸起导航 最近一直在倒腾Nuxt项目,由于Nuxt.js是基于Vue.js的服务端渲染框架,只要是会vue,基本能很快上手了. 一般 ...

  7. Qt QLineEdit 漂亮的搜索框 && 密码模式 && 格式化输入 && 提示文字 && 选择内容并移动 && 清除全部输入

    先上一个漂亮的搜索框效果图, 输入搜索文本效果, 点击搜索图标效果: //实现代码 void MainWindow::iniLineEdit() { ui->lineEdit->setPl ...

  8. 关于隐式创建vue实例实现简化弹出框组件显示步骤

    我们在使用vue写alert组件的时候,经常是定义了一个alert.vue,然后引入alert.vue,然后配置参数等等,非常繁琐,那有没有一种方式可以像window.alert("内容&q ...

  9. Flex AIR自定义Mobile的弹出框组件

    做Flex Mobile开发的人应该知道,Flex为手机应用并没有提供弹出框组件,需要自定义. 通过查找文档.资料,我做出一个效果还算不错的弹出框组件,可以适用于手机设备上,不多讲,直接贴源码,相信对 ...

  10. WeChat-SmallProgram:自定义select下拉选项框组件

    1):创建组件所需的文件 2):自定义组件 CSS 及 JS 组件的wxml: <view class='com-selectBox'> <view class='com-sCont ...

随机推荐

  1. 《花雕学AI》13:早出对策,积极应对ChatGPT带来的一系列风险和挑战

    ChatGPT是一款能和人类聊天的机器人,它可以学习和理解人类语言,也可以帮人们做一些工作,比如翻译.写文章.写代码等.ChatGPT很强大,让很多人感兴趣,也让很多人担心. 使用ChatGPT有一些 ...

  2. 事实胜于雄辩,苹果MacOs能不能玩儿机器/深度(ml/dl)学习(Python3.10/Tensorflow2)

    坊间有传MacOs系统不适合机器(ml)学习和深度(dl)学习,这是板上钉钉的刻板印象,就好像有人说女生不适合编程一样的离谱.现而今,无论是Pytorch框架的MPS模式,还是最新的Tensorflo ...

  3. xtrabackup8.0.27备份失败

    问题描述:mysql8.0.27备份出现中断,重新备份发现xtrabackup备份失败,xtrabackup与mysql版本不匹配,后来想起来时mysql进行了升级,8.0.27->8.0.29 ...

  4. 从ReentrantLock角度解析AQS

    是它,是它,就是它,并发包的基石: 一.概述 闲来不卷,随便聊一点. 一般情况下,大家系统中至少也是JDK8了,那想必对于JDK5加入的一系列功能并不陌生吧.那时候重点加入了java.util.con ...

  5. ShardingJDBC配置

    Sharding-JDBC定位为轻量级Java框架,在Java的JDBC层提供的额外服务. 它使用客户端直连数据库,以jar包形式提供服务,无需额外部署和依赖,可理解为增强版的JDBC驱动,完全兼容J ...

  6. 基于.Net开发的ChatGPT客户端,兼容Windows、IOS、安卓、MacOS、Linux

    2023年目前要说最热的点,肯定是ChatGPT了. ChatGPT官方提供的网页版本,还有需要科*上网,很多人都会基于此进行封装. 现在是移动互联网时代,基于手机APP的需求还是很大的. 所以,今天 ...

  7. 音频处理库性能对比:计算mel频谱的速度哪个更快?

    介绍 音频信号处理在各种应用中都发挥着重要的作用,如语音识别.音乐信息检索.语音合成等.其中,Mel频谱是一种常用的频域特征表示方法,用于描述人类听觉系统对频率的敏感程度. 在深度学习音频领域,mel ...

  8. OFFICE-利用Word邮件合并功能联动编辑《目标责任成本调整说明》

    正文 00.开始以及目标 0.1 开始 众所周知的原因,X建工的很多文档都提供了一个填写模板,这是个好事.但是捏,当他们把模板放下来要来填数的时候,你会发现所有的数据,都是在不同的文档中搬来搬去,这点 ...

  9. 2021-07-31:给定数组father,大小为N,表示一共有N个节点,father[i] = j 表示点i的父亲是点j, father表示的树一定是一棵树而不是森林,给定数组values,大小为N

    2021-07-31:给定数组father,大小为N,表示一共有N个节点,father[i] = j 表示点i的父亲是点j, father表示的树一定是一棵树而不是森林,给定数组values,大小为N ...

  10. 2021-08-15:给定一个字符串Str,返回Str的所有子序列中有多少不同的字面值。

    2021-08-15:给定一个字符串Str,返回Str的所有子序列中有多少不同的字面值. 福大大 答案2021-08-15: 返回值=上+新-修正. 时间复杂度:O(N) 空间复杂度:O(N). 代码 ...