原生微信小程序,搜索框(search)组件

淡淡的烟草味﹌ 2022-12-02 15:06 401阅读 0赞

原生微信小程序,搜索框(search)组件

在这里插入图片描述
首先创建组件及两个跳转页面,第一个是搜索框所在的页面(首页),第二个是搜索详细页
搜索框的本质是:搜索框不是input,而是navigate,跳转到专门的搜索页,搜索结果会呈现在这一页
先创建三个文件
在这里插入图片描述

标题selectPage页面的编写

在 selectPage.wxml 文件里引入搜索框组件
引入组件之前记得在 selectPage.json 文件中配置 组件

  1. {
  2. "usingComponents": {
  3. "search": "../../components/select"
  4. }
  5. }
  6. <!-- 父组件向子组件传递参数,使用属性的形式,子组件通过 properties进行参数接收 -->
  7. <search urlPath="{ {urlPath}}" url="../selectResult/selectResult"></search>

最后在 JS 文件中 设置url路径,因为我们的目的是制作组件,所以参数这块都由父组件传递过去
到这里,selectPage 的任务基本完成了

  1. Page({
  2. data: {
  3. urlPath: "../selectResult/selectResult"
  4. },
  5. onLoad: function (options) {
  6. }
  7. })

组件的编写

搜索组件的基本 结构层 写一下

  1. <view class="search_wrap">
  2. <navigator url="{ {urlPath}}">
  3. 搜索
  4. </navigator>
  5. </view>

css样式也随便改一改,自己看的舒服就行,这里用的是less语言

  1. /* components/select.wxss */
  2. .search_wrap {
  3. width: 100%;
  4. height: 80rpx;
  5. background-color: #eb4450;
  6. display: flex;
  7. justify-content: center;
  8. align-items: center; navigator {
  9. text-align: center;
  10. padding: 10rpx;
  11. width: 90%;
  12. background-color: #fff;
  13. color: #6b6b6b;
  14. border-radius: 20rpx;
  15. }
  16. }

JS文件中接收一下来自父元素传递过来的URL路径参数

  1. // components/select.js
  2. Component({
  3. properties: {
  4. urlPath: {
  5. type: String
  6. }
  7. },
  8. data: {
  9. },
  10. methods: {
  11. }
  12. })

selectResult 页面的编写

结构层

  1. <view class="search_row">
  2. <input value="{ {inputValue}}" bindinput="handleInput" class="inp" placeholder="请输入"></input>
  3. <button class="btn" bind:tap="handleCanle">取消</button>
  4. </view>
  5. <view class="search_content">
  6. <view class="search_item" wx:for="{ {searchResult}}">{
  7. {item.goods_name}}</view>
  8. </view>

表示层

  1. page {
  2. background-color: #ebebeb;
  3. padding: 20rpx;
  4. }
  5. .search_row {
  6. height: 60rpx;
  7. display: flex; .inp {
  8. background-color: #fff;
  9. height: 100%;
  10. padding-left: 20rpx;
  11. flex: 4;
  12. border-radius: 5rpx;
  13. }
  14. .btn {
  15. width: 110rpx;
  16. height: 100%;
  17. font-size: 26rpx;
  18. font-weight: normal;
  19. padding: 0;
  20. margin: 0 0 0 10rpx;
  21. display: flex;
  22. justify-content: center;
  23. align-items: center;
  24. }
  25. }
  26. .search_content {
  27. margin-top: 30rpx; .search_item {
  28. overflow: hidden;
  29. white-space: nowrap;
  30. text-overflow: ellipsis;
  31. background-color: #fff;
  32. border-bottom: 1px solid #7e7e7e;
  33. height: 60rpx;
  34. padding: 10rpx;
  35. font-size: 26rpx;
  36. }
  37. }

行为层

  1. // pages/selectResult/selectResult.js
  2. Page({
  3. data: {
  4. // 定义搜索结果变量,初始化为空数组
  5. searchResult: [],
  6. // 定义inputValue,输入框的input值,初始化为空
  7. inputValue: ""
  8. },
  9. handleInput: function (e) {
  10. // 获取输入框的值
  11. const { value } = e.detail
  12. // 合法性验证,去除前后空格,防止打很多空格也会发送请求
  13. // 去除空格后的值是合法值了,再取反,为不合法值,不合法搜索结果清空
  14. if (!value.trim()) {
  15. this.setData({
  16. searchResult: [],
  17. })
  18. // 值不合法,直接return掉,不用执行下面的了
  19. return;
  20. }
  21. /** * 防抖动功能 * 1 如果第一次输入的值合法,会执行this.TimeId这个定时器,然后一秒钟之后发送请求 * 2 如果在这1秒钟的延迟内,用户重新做了输入,重新触发handleInput方法时,会执行clearTimeout * 将上一个定时器请求杀掉,然后再执行新的定时器函数,直到用户1庙后都没有新的输入后,才会发送请求 */
  22. clearTimeout(this.TimeId)
  23. this.TimeId = setTimeout(() => {
  24. this.qSearch(value)
  25. }, 1000)
  26. },
  27. // qSearch 发送请求函数(参数为输入框中的值)
  28. qSearch: function (query) {
  29. // 定义一个加载中的提示
  30. wx.showLoading({
  31. title: '加载中...',
  32. })
  33. // 发送请求wx.request,微信封装好的ajax请求
  34. // 参数有两个需要传递的
  35. // url,接口地址
  36. // data,搜索的关键词,这边接口会自动处理并返回带有关键词的内容,
  37. // 不是因为wx.request又会发送ajax,还会给你自动筛选
  38. wx.request({
  39. url: 'https://api-hmugo-web.itheima.net/api/public/v1/goods/qsearch',
  40. data: { query },
  41. // 请求成功时调用
  42. success: (result) => {
  43. // 将获取的值,赋值给定义的searchResult,WXML文件获取渲染
  44. this.setData({
  45. searchResult: result.data.message
  46. })
  47. // 成功获取数据后,把加载中的提示关掉
  48. wx.hideLoading({
  49. complete: (res) => { },
  50. })
  51. }
  52. });
  53. },
  54. // 取消按钮函数
  55. handleCanle: function () {
  56. this.setData({
  57. searchResult: [],
  58. inputValue: ""
  59. })
  60. }
  61. })

gitee代码地址:https://gitee.com/chenminghuisir/wechat-applet-component.git
代码保存在仓库,WXDoubleScroller文件里

发表评论

表情:
评论列表 (有 0 条评论,401人围观)

还没有评论,来说两句吧...

相关阅读