


Teach you step by step how to implement content search function in mini program
This article will share with you a practical experience in developing a small program and introduce how to implement the content search function in a small program. I hope it will be helpful to everyone!
Previously we implemented the display of the homepage content list, but did not implement the real function of the search box at the top. In this article, we will add a real search to the search box. Function. [Related learning recommendations: 小program development tutorial]
Search content binding
Let’s take a look at what it looks like after we click on the search box now
Here, we use vant's search
component, and its documentation introduces a series of event binding methods for this component.
What we want to use is to trigger the real content search when the search is determined, so we add the binding of this event to the search
component As follows
Next, we print the value variable bound to the search component in the response implementation of the search event, and then perform input testing.
handleSearch() { const { searchValue } = this.data console.log('搜索内容', searchValue) }
It should be noted here that in the developer tools, for components such as input boxes, since they are on the computer side, they cannot evoke an input method interface similar to the mobile phone side, so the interaction with the small program on the mobile phone is All real differences.
In this case, we can use the preview function to debug on the mobile phone. After opening the mini program on the mobile phone, you can click ...
in the upper right corner to bring up the debugging panel to view the console. Output.
Or use the real machine debugging
function provided by the developer tools. You can preview the mini program on your mobile phone and see the debugging information in real time in the developer tools on the computer.
Debugging
After debugging, we found that the value
bound to search
will only be entered for the first time It is later changed and stored in searchValue
, and the value entered subsequently will not update this variable. This results in us not being able to use the latest entered content for every search, which is a problem.
Data Binding
The solution is also very simple, which is to use the data binding feature that comes with the mini program and add the search
component to the The incoming value
attribute is changed to model:value
, thereby enabling two-way data binding.
In this way, after subsequent input updates, the value of searchValue
will be updated at the same time to achieve the effect of synchronous modification.
Conditional query
Then we can get the content entered in the search box each time, and we can use this as a keyword to query the list content.
The rule is to find the records containing our search keywords in text
from all the records in the database. Of course, only 20 records will be returned at a time, and pull-up can also be supported. load.
In fact, the method here is basically the same as the previous method of querying data, except that a keyword matching condition is added on the original basis, so let us transform the previous data query method.
We added the search content as a parameter to the method originally used to refresh the entire list content, and passed it transparently to the cloud function processing method used to actually query the data. At the same time, every time the search box confirms the search, it is judged whether there is search content, and if so, the data is reacquired.
Database record matching
Next, we also need to make corresponding changes to the database data extraction in the cloud function. The database record will be used here. Regular matching
method is used to match the search content. For details, please refer to the official document
The core logic after the transformation is as follows
const db = cloud.database() const collection = db.collection('homeContentList') let searchPromise let countPromise try { if (content) { const searchReg = db.RegExp({ regexp: content, options: 'i' }) searchPromise = collection.where({ text: searchReg }) .skip(pageNo).limit(pageSize).get() countPromise = collection.where({ text: searchReg }) .count() } else { searchPromise = collection.skip(pageNo).limit(pageSize).get() countPromise = collection.count() } const [{ data: listData }, { total }] = await Promise.all([searchPromise, countPromise]) if (listData) { data = listData } totalSize = total } catch (error) { console.log('error', error) }
We determine whether there is search content , to distinguish whether to perform regular matching in the process of querying data, and ignore the letter case of the search content when there is search content, so as to match as much content as possible.
Search results
In this way, we realize the query for the search content.
内容页面优化
最后,我们优化一下首页的展示效果。
无数据组件
很多页面都有可能是数据列表的形式,而其各自在加载完所有数据后需要展示在底部的“没有更多内容”字样可能不同,所以我们将这部分展示封装成一个可供复用的自定义组件。
试图部分
<view class="wrap"> <text class="text">{{text}}</text> </view>
组件样式
.wrap { width: 100%; padding: 20rpx 0 40rpx; text-align: center; } .text { color: #999; font-size: 26rpx; line-height: 30rpx; }
组件参数
Component({ properties: { text: { type: String, value: '没有更多内容了' } } })
展示效果
总结
这篇,我们实现了搜索框功能,将搜索框中输入的内容作为搜索关键字,从而在数据库中查找包含关键字的记录进行展示。另外,我们还将“没有更多内容”封装可供复用的组件。
更多编程相关知识,请访问:编程入门!!
The above is the detailed content of Teach you step by step how to implement content search function in mini program. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











With the popularity of mobile Internet technology and smartphones, WeChat has become an indispensable application in people's lives. WeChat mini programs allow people to directly use mini programs to solve some simple needs without downloading and installing applications. This article will introduce how to use Python to develop WeChat applet. 1. Preparation Before using Python to develop WeChat applet, you need to install the relevant Python library. It is recommended to use the two libraries wxpy and itchat here. wxpy is a WeChat machine

Implementing card flipping effects in WeChat mini programs In WeChat mini programs, implementing card flipping effects is a common animation effect that can improve user experience and the attractiveness of interface interactions. The following will introduce in detail how to implement the special effect of card flipping in the WeChat applet and provide relevant code examples. First, you need to define two card elements in the page layout file of the mini program, one for displaying the front content and one for displaying the back content. The specific sample code is as follows: <!--index.wxml-->&l

According to news from this site on October 31, on May 27 this year, Ant Group announced the launch of the "Chinese Character Picking Project", and recently ushered in new progress: Alipay launched the "Chinese Character Picking-Uncommon Characters" mini program to collect collections from the society Rare characters supplement the rare character library and provide different input experiences for rare characters to help improve the rare character input method in Alipay. Currently, users can enter the "Uncommon Characters" applet by searching for keywords such as "Chinese character pick-up" and "rare characters". In the mini program, users can submit pictures of rare characters that have not been recognized and entered by the system. After confirmation, Alipay engineers will make additional entries into the font library. This website noticed that users can also experience the latest word-splitting input method in the mini program. This input method is designed for rare words with unclear pronunciation. User dismantling

Mini programs can use react. How to use it: 1. Implement a renderer based on "react-reconciler" and generate a DSL; 2. Create a mini program component to parse and render DSL; 3. Install npm and execute the developer Build npm in the tool; 4. Introduce the package into your own page, and then use the API to complete the development.

How uniapp can achieve rapid conversion between mini programs and H5 requires specific code examples. In recent years, with the development of the mobile Internet and the popularity of smartphones, mini programs and H5 have become indispensable application forms. As a cross-platform development framework, uniapp can quickly realize the conversion between small programs and H5 based on a set of codes, greatly improving development efficiency. This article will introduce how uniapp can achieve rapid conversion between mini programs and H5, and give specific code examples. 1. Introduction to uniapp unia

This article brings you some related issues about WeChat mini programs. It mainly introduces how to use official account template messages in mini programs. Let’s take a look at them together. I hope it will be helpful to everyone.

Implementation idea: Establishing the server side of thread, so as to process the various functions of the chat room. The establishment of the x02 client is much simpler than the server. The function of the client is only to send and receive messages, and to enter specific characters according to specific rules. To achieve the use of different functions, therefore, on the client side, you only need to use two threads, one is dedicated to receiving messages, and the other is dedicated to sending messages. As for why not use one, that is because, only

Geolocation positioning and map display of PHP and mini programs Geolocation positioning and map display have become one of the necessary functions in modern technology. With the popularity of mobile devices, people's demand for positioning and map display is also increasing. During the development process, PHP and applets are two common technology choices. This article will introduce you to the implementation method of geographical location positioning and map display in PHP and mini programs, and attach corresponding code examples. 1. Geolocation in PHP In PHP, we can use third-party geolocation
